This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Network and Internet
.NET 2.0+

Sending SMTP Email

Many modern applications send email for a variety of purposes. These include sending email to customers, suppliers and other businesses or individuals or for reporting problems that have occurred whilst running a program. With .NET, sending mail is easy.

Simple Mail Transport Protocol

Simple Mail Transport Protocol (SMTP) is the standard for sending email via the Internet. It is used to send email messages to one or more recipients, including carbon copied and blind carbon copied recipients, using an SMTP server. The server relays the message to the correct destinations using the email addresses specified.

The .NET framework provides the System.Net.Mail namespace for SMTP email functionality. This namespace contains classes that are useful when sending email. Three key classes are SmtpClient, MailMessage and Attachment. These were introduced in .NET framework 2.0.

SmtpClient Class

The SmtpClient class provides the functionality required to send email using an SMTP server. This class is configured with the details of the SMTP server to be used, including security credentials where required. It is possible to send plain text email using this class alone.

MailMessage Class

The MailMessage class represents an email message that can be sent using an SmtpClient object. It allows more complex email messages than with an SmtpClient object alone. Messages can include HTML-formatted text, attachments and prioritisation.

Attachment Class

The Attachment class is used with MailMessage objects to add attachments to an email. Using Attachment objects, any type of file may be sent with an email message.

Sending Email in .NET

The following sections explain how to send email from a .NET application. To begin, create a new console application named "SmtpDemo". Add the following using directive to the code:

using System.Net.Mail;

Configuring an SmtpClient

When sending email using an SmtpClient object, the object must be configured to communicate with an SMTP server. The server may be connected to a local network, such as a Microsoft Exchange server, or be a server on the Internet. The server to be used is configured in the Host property of the SmtpServer object. The property holds a string containing the server's name or IP address.

In the examples, we will hold the host name in a constant. To define the constant, add the following line in the Program class's code block, substituting "hostname" for the name of your SMTP server. NB: If you download the sample code, you must modify the constants before executing the program.

const string SmtpHost = "hostname";

Within the Main method, we can now create a new SmtpClient object and specify the host name. Add the following code to the method:

SmtpClient client = new SmtpClient();
client.Host = SmtpHost;

Configuring the Server Port

Most SMTP servers are configured to send email using port 25, though this can be modified in the server's configuration. If the system administrator has modified the port, the correct port must be specified in the SmtpClient's Port property. If your port configuration is non-standard, add the following constant to the class, using the correct port number:

const int SmtpPort = 25;

To use the configured port, set the Port property of the SmtpClient object as follows:

client.Port = SmtpPort;

Configuring Credentials

Some SMTP servers permit the sending of email without authentication. If the server that you are using requires authentication there are two ways to provide it. The first is to use the login details of the current user. This is achieved by setting the UseDefaultCredentials property to true.

If your SMTP server requires authentication and the default credentials are suitable, add the following line to the Main method:

client.UseDefaultCredentials = true;

In some cases, a specific set of credentials must be provided before the server will send email. In these cases, the UseDefaultCredentials property will be insufficient. Instead, a specific login name and password must be provided using a NetworkCredential object.

The NetworkCredential class is found in the System.Net namespace. If your SMTP server requires specific credentials to be supplied, ensure that you have added using System.Net; to the top of the code file before adding the following code to the Main method. Substitute "username" and "password" with the appropriate security information.

NetworkCredential cred = new NetworkCredential();
cred.UserName = "username";
cred.Password = "password";

NB: When using specific credentials, do not set the value of UseDefaultCredentials to true.

Some More Constants

In the examples that follow we will send email in a variety of ways. The email addresses that you use for sending and receiving email will be different from any that could be defined here. To make the samples easier to read and simpler to copy and paste into your own code, we need constants to hold these addresses.

We will be using five email addresses in the examples. These will be the sender's email address, two recipient addresses and recipients for carbon copies and blind carbon copies. If you do not have the use of five email addresses you may want to duplicate some or use free web-based email accounts.

Add the following constants to the Program class, specifying valid email addresses for each.

const string SenderEmail = "test@...";
const string RecipientEmail1 = "test1@...";
const string RecipientEmail2 = "test2@...";
const string CcEmail = "testcc@...";
const string BccEmail = "testbcc@...";
21 April 2008