Tutorial: Email sending with VB.Net


So you want to send email from your vb.net application? No problem!
A lot of times an email is sent in the “background” with pre-configured settings. These could be hard-coded, or stored in the Application/User Settings file. If you want to have fields in your WinForm that ask the user for the information, the concept will be the same, but with the additional step of validation, which is always a good thing 🙂

Let’s start with creating a new mail message:

Dim email As New MailMessage

This creates a new MailMessage object which we can then set the properties of before sending the email.
Next, we need to set the arguments for the Send method so that it is sent properly. We will need to properties for: From address, To address, Subject, and the body of the email.

From: Email address you want to appear at the recipient’s end and the name associated with the email address
Example – .From = New MailAddress(“jdoe@email.com”, “John Doe”)

To: Email address you want to send the email to and the name you want to appear (usually their full name)
Example – .To.Add(New MailAddress(“recipient@email.com”, “Jane Doe”))

Here, you will notice that there is an Add method which you can use to add multiple recipients to an email message. All you need to do is add another line like above for each recipient. If you had a list with email addresses, you could iterate through the list with a for loop adding the recipients one at a time:

.To.Add(New MailAddress("recipient1@email.com", "Jane Doe"))
.To.Add(New MailAddress("recipient2@email.com", "James Doe"))
.To.Add(New MailAddress("recipient3@email.com", "Jenny Doe"))

Subject: This is what will appear as the subject for the email (hard stuff huh?)
Example – .Subject = “I’m Sending Emails from my WinForm!”

Body: This is what will appear as the body, or message, of the email (it’s almost intuitive! /sarcasm)
Example – .Body = “Thanks to www.stateofidleness.com, I can send emails from my Windows app.”

So with these in mind, let’s code it in our application.
We can do this using a With structure:

With email
       .From = New MailAddress("jdoe@email.com", "John Doe")
       .To.Add(New MailAddress("recipient1@email.com", "Jane Doe"))
       .Subject = "I'm Sending Emails from my WinForm!"
       .Body = "Thanks to www.stateofidleness.com, I can send emails from my Windows app."
End With

Now the “hard” part. You will need to obtain your SMTP settings from your ISP or other email provider.

We’ll create a new SmtpClient which is required for sending the email

Dim mySMTP As New SmtpClient("smtp.server.address.com") 'You will put your SMTP server here

Now we need to put in our settings for Port, Credentials and whether we use SSL or not:

mySMTP.Port = 25 'Change to whatever port your SMTP server uses
mySMTP.EnableSsl = False 'True or False depending on your SMTP server
mySMTP.Credentials = New System.Net.NetworkCredential("me@test", "password") 'Enter your username and password you use when sending email

These are the settings that would be most likely kept in a config file and not hard-coded, as a server address may change, or a username and password will most likely change. You don’t want to have to recompile every time your password changes.

We’re almost done! All we have to do now is send the thing! To do this, we call the Send method and pass it our email object:

mySMTP.Send(email)

That’s it! Now let’s look at the code all together:

Dim email As New MailMessage
With email
       .From = New MailAddress("jdoe@email.com", "John Doe")
       .To.Add(New MailAddress("recipient1@email.com", "Jane Doe"))
       .Subject = "I'm Sending Emails from my WinForm!"
       .Body = "Thanks to www.stateofidleness.com, I can send emails from my Windows app."
End With

Dim mySMTP As New SmtpClient("smtp.server.address.com") 'You will put your SMTP server here
mySMTP.Port = 25 'Change to whatever port your SMTP server uses
mySMTP.EnableSsl = False 'True or False depending on your SMTP server
mySMTP.Credentials = New System.Net.NetworkCredential("me@test", "password") 'Enter your username and password you use when sending email
mySMTP.Send(email) 'Bon Voyage!

And off it goes! That’s all there is to it. Hopefully this tutorial helps you in sending emails from your VB.Net apps.

2 thoughts on “Tutorial: Email sending with VB.Net

  1. Thanks for the Tutorial: Email sending with VB.Net

    This is one of the many techniques that I was researching before plunging in to rewrite some of my lagacy apps.

    You are most generous to provide this and it is a great help to me – thank you!

    A real bonus would be some thoughts/code on how you would handle incoming messages?

    Regards

    Raymond (London UK)

  2. Not wanting to hijack someone else’s blog post but in response to the comment above regarding handling incoming emails – I am writing a .NET SMTP server library at the moment which will include a class named SmtpReceiver. This SmtpReceiver class will basically listen for incoming SMTP requests (incoming emails) and will respond to the remote client that is trying to send the email to you so that the email is successfully submitted, then it will raise an event that your program can handle and will pass an object to this event that contains all of the inbound email details (such as who the email was sent to, who it was from, the body of the email etc). Here’s some more information on it: http://cjwdev.wordpress.com/2009/11/06/my-smtpreceiver-class/

Leave a Reply

Your email address will not be published. Required fields are marked *