Send an Email from a .NET Application

Introduction

Have you ever wanted to setup your application to send an email in response to a particular event? If you have chances are that you're developing an ASP.NET application but really an email can be sent from any kind of application. Perhaps you've setup your website to collect information from users to subscribe them to your newsletter and you want to send a confirmation email when they do. Another possibility is you've setup a shopping cart and you want to send the order confirmation when the user completes their transaction. Whatever the reason you may have for sending an email the truth is that it does not take a lot of code to actually send one. Follow along as I show you how.

In order to send an email when using .NET 2.0 and above you will be using a handful of classes in the System.Net.Mail namespace. The classes you will use in this namespace are MailMessage, MailAddress, MailAddressCollection and SmtpClient. Example 1-1 shows how to setup the actual email message. After first creating an instance of the MailMessage class we add the addresses which we want to send the email to. Notice how the "To" property of the MailMessage is a collection. Therefore, we can add multiple addresses to send to. You have the option of just specifying an email address or you can add an instance of the MailAddress class which gives you more control like being able to specify the display name as well as the email address. Next we set the subject of the email which is a simple string. Next we set the who the email address is from. The "From" property is an instance of MailAddress. Normally you will want to specify the email address as well as a display name as this looks a more professional in my opinion. The display name is what shows when the user is looking at all the emails in their inbox. If you don't set the display name then the email address will be displayed. Lastly, we set the actual body of the email. The "Body" property is a string. Constructing the body string can be done however you want. You can add html markup if you prefer but remember to set the IsBodyHtml property to true if you do otherwise the email will be sent as plain text and all that markup will be displayed in the body of the email.

Example 1-1

1// Create an instance of the MailMessage class

2System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

3 

4// Add the addresses of who you want to send the email to

5message.To.Add( "johndoe@msn.com" );

6message.To.Add( "johnsmith@msn.com" );

7 

8// Set the subject of the email

9message.Subject = "Order Confirmation";

10 

11// Set the address and name of the sender.

12// Note this does not have to be the same

13// as the email account through which you're sending the email

14message.From = new System.Net.Mail.MailAddress( "confirmation@msn.com", "Sales Confirmation" );

15 

16// Add the body of the email.

17message.IsBodyHtml = false;

18message.Body = "This is the body of the email.";

Now that the message is established you have to send it. That's where the SmtpClient class comes in. This class provides all the code necessary to connect to your email server and send the email. Example 1-2 shows how to use it. Naturally you must provide credentials to an existing email on that server. To do that you setup an instance of System.Net.NetworkCredential. You will need to provide three pieces of information. First you need to provide the server which you will send the email through. Then you need to give the username and password of a valid email account on that server. Once that is setup you only need three more lines of code to actually send the email.

Example 1-2

1// Setup the credentials information

2System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();

3 

4// The domain property must be set to the domain of your email server

5credentials.Domain = "mail.msn.com";

6 

7// UserName and Password are for the actual email account on the server

8// that you will send the email through. Note as above, the email address

9// that the recipient will receive from can be different from the email address

10// account which you're sending through.

11credentials.UserName = "generalemail@msn.com";

12credentials.Password = "123456";

13 

14// Instantiate the SmtpClient, set the credentials and send the email

15System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();

16client.Credentials = credentials;

17client.Send( message );

Conclusion

That's it! Everything you need to send an automated email message through your application.

New Comment

Info

Your name and comment are required. Enter an email and url if your prefer. Your email will not be displayed.

Published: 10/13/2008
View Code:
Email
 

.NET Advisor