New Mail Message

Send Email Walkthrough: for C# and VB NET Students


In the last lesson, you've set up your SmtpClient. Now it's time to create a Mail Message. This is done through the MailMessage Class. Add this to your code, after the smtpServer.Credentials line of your BtnSend code:

VB Net

Dim message As New MailMessage( emailFrom, emailTo )


MailMessage message = new MailMessage(emailFrom, emailTo);

So we're setting up an object called message, which is a MailMessage object. In between the round brackets of MailMessage we first specify who is sending the email. After a comma, we specify who the email is going to. We got these details from the textboxes.

You can also specify an encoding type for your email:

message.BodyEncoding = System.Text.Encoding.UTF8

Here, we're setting the Encoding to UTF8. Others are ASCII, UTF7 and UTF32.

These days, if your email has no Headers, it may get flagged as spam. You can add a Reply-To header and an Organisation header (again, semicolon on the end, for C#):

message.Headers.Add( "Reply-To", email_from )
message.Headers.Add( "X-Organization", "Home and Learn" )

We're almost ready to send the email. However, there's a slight problem: We have no way of knowing if the email has been sent. If there's a problem with, say, the credentials, we need Visual Studio to report back to us. Otherwise, it will appear as though nothing has happened, even though no email will get through to the recipient.

The way you get Visual Studio to report back is a bit complicated. But you need to add something called an Event Handler. This allows you to use the SendCompleted event of the SmtpClient object you set up. You tell the Event Handler which Subroutine or method you want to call when the Sending of the email has been Completed.

Add this line to your code for VB Net:

AddHandler smtpServer.SendCompleted, AddressOf DoSendCompleted

And this is C#:

smtpServer.SendCompleted += new SendCompletedEventHandler(DoSendCompleted);

The AddHandler or SendCompletedEventHandler part means "Add an Event Handler". The AddressOf part tells VB which Subroutine to call when the email has been sent. In C#, the method to call is in the round brackets of SendCompletedEventHandler. In both cases, this Sub or method has the name DoSendCompleted, which is just something we made up. We could have called it anything.

You need to add this Subroutine (method in C#) yourself. So, after the End Sub of you button code, add the following Sub in VB Net:

VB Net code for an event handler function

And here it is in C#.

C# code for an event handler method

In between the round brackets of DoSendCompleted, in both VB Net and C#, there is an event called AsyncCompletedEventArgs. It has the variable name of e. It means you can access methods called UserState, Cancelled, and Error. It's basically listening out to what happens when your email is sent.

This method/Sub we've created means that a message box will be displayed when the email has been sent. The UserState in the code above is used to identify which email was sent.

You will probably have noticed the curious token string variable we've set up in the code. To see why, add these two final lines of code to your Send button:

VB Net

Dim userState As String = " - Mail Message"
smtpServer.SendAsync( message, userState )


string userState = " - Mail Message";
smtpServer.SendAsync(message, userState);

Wrap the last lines in a Try … Catch block, so that you'll get some feedback if anything goes wrong:

VB Net


smtpServer.SendAsync(message, userState)

Catch ex As Exception


End Try




smtpServer.SendAsync(message, userState);

catch (Exception ex)



An error message you may get is something like this one:

Message box displayed when the smtp host could not be found

First, we've set up a string variable (string object) called userState. The Send method we're using is SendAsync. The Async stands for Asynchronous, and means the programme is not kept waiting for the email to be sent - it can get on with other things while sending. However, it needs two things, the message to send, and a UserState token. The token can be just about any object. But we're using a string.

The result of all this is that Visual Studio sends the email message using the SMTP details you set up. If there's a problem, you'll see an error message:

General error message when the email server can't log in

If all is OK, then the message will be this:

Message box stating that the email has been sent

With the problem email, we didn't fill in our credentials correctly. Although the error message we got back is a bit mysterious, at least we got something back!

But the whole of your email code should look something like this one in VB Net:

VB Net code for sending an email through an SMTP server

And here's the C# code:

C# code for sending an email through an SMTP server

You can test out your programme, now. Hopefully, you'll be able to send emails. Or at least get a message as to why not. (It's probably the port number for smtp, or your username and password. Or the Host. Yeah, lots of things can go wrong. But don't give up!)

In the next lesson below, you'll learn how to add attachments to your emails.

Add an Attachment >>

Back to the C# NET Contents Page

Back to the VB NET Contents Page