September 03, 2010   Login   |   Register  
  Search  
  |  
  Tips & Tricks | Developers Corner | Email Avenger

Email Avenger

Minimize

Purpose of Email Avenger

  1. Show that you can send email with something other than ASP
  2. Show how to consume a COM server
  3. Send email when your email server is down (thanks to all the f%*&@g spammers!)
  4. Frame that bastard in the office ;-)

Program Summary

Email Avenger is a simple email client that allows you to send email without an email account or email server.

Programming Summary

We'll use VB.NET to look at consuming COM servers and use a COM server to send email.

Email Avenger Requirements

Visual Studio .NET 2003 (VB.NET)
w3 JMail 4.4
VB.NET resource kit with Component1 components installed
a brain

Down to Business...

Email Avenger will be a stand alone email program that will allow the user to send email from anyone to anyone without an email server.

Now, you should be asking yourself, “Isn't this just another spam program?” Well, no. We're not going to allow that kind of scale in Email Avenger. The primary purposes are going to be:

  1. Send an email when your email server is down
  2. Send an email without your email server because the receiver is having problems receiving email from you
  3. Send anonymous emails to someone you hate, etc.

Now that we know what we want, let's do some setup.

We're going to use w3 JMail 4.4 to send mail. So if you haven't already, download and install it from the Dimac site: http://www.dimac.com

You can also just use the DLL in the source download. Simply place it in some directory (usually system32) and register it from a command prompt. (regsvr32 jmail.dll)

There is a very good reason why we are using JMail... we don't want to reinvent the wheel. There are times when reinventing the wheel is the right thing to do, but they are very rare. If you need this article, then I seriously doubt that you need to reinvent the wheel. If you can, always use commercially available (and sometimes free) components. It will save you a lot of time and aggravation.

Open Visual Studio .NET 2003 (VS) and create a blank project. Name it 'EmailAvenger'. Right click on the solution in the Solution Explorer and choose Add -> VB Projects -> Windows Application and name it EmailAvenger. Click Form1.vb in the Solution Explorer and in the Properties box rename it to EmailAvenger.vb.

Click the form in designer mode to select it, and in the Properties box change the Name value to 'frmEmailAvenger' and the text value to 'Email Avenger'. Now we've got our names all nice and clean.

Right click on 'References' and choose Add Reference. This is how we will add JMail to our project.

Click the COM tab and scroll down to 'JMail 4.0 library', Select it, click Select, and click OK.

JMail is 'almost' available now... let's finish it up and make it fully available.

Right click EmailAvenger.vb in the Solution Explorer and choose View Code.

At the top of the code above the 'Public Class EmailAvenger' declaration, insert this line:

Imports JMail

That will tell VS that we want to use the jmail.dll COM server in our program. Pretty damn simple, eh?

Now it's time to add stuff to our form and then add a bit of code.

Drag and drop a button, 8 labels, 6 text boxes, a rich text box, and a Component One numeric up/down input.

That's all we need for stuff on our form. Arrange them somthing like as follows:

Now that we've got our form setup, let's change the names and text to something a bit more meaningful. In the Properties window for each component, change them as follows:

Original Control Name New Name New Text/Value
Button1 btnEmailAvenger Send
Label1 lblNumberToSend # to send:
C1NumericEdit1 c1nudEmailAvengerNumber 0
Label2 lblSent 0
Textbox1 txtEmailAvengerSent 0
Label3 lblEmailAvengerFailed Failed:
Textbox2 txtEmailAvengerFailed 0
Label4 lblEmailAvengerStatus Status:
Textbox3 txtEmailAvengerStatus Composing...
Label5 lblEmailAvengerTo To:
Textbox4 txtEmailAvengerTo  
Label6 lblEmailAvengerFrom From:
Textbox5 txtEmailAvengerFrom  
Label7 lblEmailAvengerSubject Subject:
Textbox6 txtEmailAvengerSubject Enter Subject Here
Label8 lblEmailAvengerMessage Message:
Richtextbox1 rtxEmailAvengerMessage Enter Message Here

And your Email Avenger Windows form will look something like this:

For the txtEmailAvengerSent, txtEmailAvengerFailed, and txtEmailAvengerStatus, go into the properties box and set their ReadOnly property to True. They are only for reporting, and we don't want the user to change them.

So, by this point we have really only done two things:

  1. Added our JMail COM server to the form
  2. Added a few controls to the form

And now we're ready to add a few lines of code for a fully functional stand-alone email program. (It will only send text, but we won't worry about that for now. Suffice it to say that it will work.)

Double click the Send button to create a Click event handler in the form code.

    Private Sub btnEmailAvenger_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmailAvenger.Click

    End Sub

Since I'm being sloppy, I'll just initialize everything in the Click event. We're only interested in actually sending the email, so don't take this as an example of good coding style.

Add the following code to initialize our values, 'pause' the form while sending, set some of our final values, and re-enable the form when we are finished:

        ' Disable the input controls when we start
        btnEmailAvenger.Enabled = False
        c1nudEmailAvengerNumber.Enabled = False
        txtEmailAvengerTo.Enabled = False
        txtEmailAvengerFrom.Enabled = False
        txtEmailAvengerSubject.Enabled = False
        rtxEmailAvengerMessage.Enabled = False

        ' Set or reset the values we want to start with
        txtEmailAvengerSent.Text = "0"
        txtEmailAvengerFailed.Text = "0"
        txtEmailAvengerStatus.Text = "Sending... Please wait..."

        ' Set our final values that have not yet been done
        txtEmailAvengerStatus.Text = "Completed with " & txtEmailAvengerSent.Text & " sent and " & txtEmailAvengerFailed.Text & " failed."

        ' Re-enable the input controls when we finish sending
        btnEmailAvenger.Enabled = True
        c1nudEmailAvengerNumber.Enabled = True
        txtEmailAvengerTo.Enabled = True
        txtEmailAvengerFrom.Enabled = True
        txtEmailAvengerSubject.Enabled = True
        rtxEmailAvengerMessage.Enabled = True

That's just fluff really. Let's add in some good stuff...

Here's the code we'll add:

        ' Do the application events so that things update properly
        Application.DoEvents()

        ' Create our JMail speedmailer object
        Dim emailAvenger As New jmail.SpeedMailer
        ' A few counters we'll use - note that only the intCounter is necessary - we could cut down on the others if we wanted to
        Dim intSent As Integer
        Dim intFailed As Integer
        Dim intCounter As Integer

        ' We may want to send more than one of the same mail to the same person... Muahahahaha~!
        For intCounter = 1 To c1nudEmailAvengerNumber.Value

            ' Some really simple error checking
            Try

                ' Send the email with the JMail object
                emailAvenger.SendMail(txtEmailAvengerFrom.Text, txtEmailAvengerTo.Text, txtEmailAvengerSubject.Text, rtxEmailAvengerMessage.Text)
                ' Increment our successful emails and display it for the user
                intSent += 1
                txtEmailAvengerSent.Text = intSent.ToString

            Catch ex As Exception

                ' Poopy... it failed... Increment the failed emails and display it
                intFailed += 1
                txtEmailAvengerFailed.Text = intFailed.ToString

            End Try

            ' Add a dot to the end of the status so people know that it is actually doing something - this would be better with a progress bar
            txtEmailAvengerStatus.Text &= "."

            ' Do the application events so that things update properly
            Application.DoEvents()

        Next

Let's walk through some of that.

The Application.DoEvents forces the form to refresh any values. If we were to send 10 or 20 emails to someone, the form might wait until we are finished all of them. This way we keep the user in the loop with what is going on.

The Dim emailAvenger As New jmail.SpeedMailer simply creates an instance of our JMail object that we will use. Later, we'll actually use it.

The For... Next loop is to let us send multiple emails... just in case we really hate someone... ;-) We get the value from the Component One numeric edit. We just dragged and dropped it onto the Windows form and it is ready to go. All the code is generated by Visual Studio automatically in the Windows Form Designer generated code region.

The Try... Catch just provides us with some very basic error checking. We should verify the email addresses, but if the user is a complete moron, screw 'em. They shouldn't be playing with this program because it is a bit open to abuse anyways and if they are that dumb, they don't deserve our help.

Everything else is pretty simple, so I'm only going to go over 1 more thing:

emailAvenger.SendMail(txtEmailAvengerFrom.Text, txtEmailAvengerTo.Text, txtEmailAvengerSubject.Text, rtxEmailAvengerMessage.Text)

Here we use the SendMail method of the JMail SpeedMail object and pass several values to it:

  1. Our from email address - txtEmailAvengerFrom.Text
  2. Our to email address - txtEmailAvengerTo.Text
  3. Our subject - txtEmailAvengerSubject.Text
  4. Our message - rtxEmailAvengerMessage.Text

If that succeeds, we increment and display the number of successes, and if not, the Catch increments and displays the failures.

Build the project, and give her a spin.

With only 1 free component, we've just built ourselves a functional emailer that we can use and abuse however we like. And instead of doing any nasty type conversions, we just dropped in a Component One numeric edit so we are guaranteed that we will only have numbers. However, we could have decimal numbers. I'll leave that to you to fix up. It's less than a line of code.

There is a lot more functionality to JMail, and we barely even scratched the surface. However, we did learn a few things:

  1. How to add a COM server to a project
  2. How to consume that COM server by creating an instance of it
  3. How to send an email without ASP or ASP.NET

Cheers and enjoy~!

Renegade

 Print   
     
Renegade Minds Web Media Basics  | 5.1 Audio on the Web | Email Avenger  | Web Form Submitter | HTTP Headers | HTTP_USER_AGENT

  Search

Copyright 2010 by Renegade Minds   |   Privacy Statement   |   Terms of Use
Renegade Minds