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
2System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
3
4
5message.To.Add( "johndoe@msn.com" );
6message.To.Add( "johnsmith@msn.com" );
7
8
9message.Subject = "Order Confirmation";
10
11
12
13
14message.From = new System.Net.Mail.MailAddress( "confirmation@msn.com", "Sales Confirmation" );
15
16
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
2System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
3
4
5credentials.Domain = "mail.msn.com";
6
7
8
9
10
11credentials.UserName = "generalemail@msn.com";
12credentials.Password = "123456";
13
14
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.