BlackWaspTM

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.

Sending a Plain Text Email

You now have an SmtpClient object named "client" that is configured for your environment. We can test that the client is correctly configured by sending an email using the Send method. To send a plain text email, the Send method can be called with four string parameters. The parameters are:

  • From Email Address. As the server is not aware of any email address of the current user, the email address that will appear in the "From" line must be specified.
  • To Email Address. The intended recipient.
  • Subject. The subject of the email.
  • Body. The text that will appear in the body of the email. This is plain text so no formatting is permitting. Some escape characters can be used. For example, using "\n" adds a line break.

To send a plain text email, execute the following code. This should appear directly after the configuration of the SMTP client:

client.Send(SenderEmail, RecipientEmail1, "Hello", "Hi.\nHow are you?");

Troubleshooting

If the above sample code fails to execute or the email does not arrive, check the following:

  • Is SMTP email blocked by the server you have used?
  • Is the sender's email address valid? Some servers will only relay from known addresses.
  • Is the recipient's email address correct?
  • Is the SMTP server's host name correct and resolvable? You may want to try the IP address instead.
  • Is the SMTP port correct? This may have been reconfigured.
  • Are the credentials you have supplied valid?

Using the MailMessage Class

As we have seen, you can send SMTP email a .NET application using only the SmtpClient class. However, this only permits plain text email and has limited functionality. A second variation of the SmtpClient's Send method accepts a MailMessage object as its parameter, increasing flexibility.

To send a simple email, similar to the one sent in the previous example, four properties of the MailMessage class are used. These are the From, To, Subject and Body properties. Unlike with the previously used overloaded version of the Send method, the four properties are not all strings.

The From property uses a MailAddress object as its value. The MailAddress class holds an email address. This email address can be passed in the object's constructor.

The To property holds a MailAddressCollection object. This collection class can hold many MailAddress objects, allowing you to send the same email to several recipients.

The Subject and Body properties are strings. As in the earlier example, the Body text can include control characters such as carriage return escape codes.

To use the MailMessage class to send an identical message to the previous example, run the following code. This should appear after the "client" object is configured. Note how the From and To properties are initialised.

MailMessage message = new MailMessage();
message.From = new MailAddress(SenderEmail);
message.To.Add(new MailAddress(RecipientEmail1));
message.Subject = "Hello";
message.Body = "Hi.\nHow are you?";
client.Send(message);

Using Display Names

MailAddress objects contain a property that permits you to specify a real name in addition to an email address. In most email reader applications these user-friendly names are displayed instead of, or alongside, the email addresses of the recipient and sender. The DisplayName property can be set independently or, as shown below, within the object's constructor.

MailMessage message = new MailMessage();
message.From = new MailAddress(SenderEmail, "Richard");
message.To.Add(new MailAddress(RecipientEmail1, "Lisa"));
message.Subject = "Hello";
message.Body = "Hi.\nHow are you?";
client.Send(message);

Adding Multiple Recipients

As the To property of the MailMessage class holds a collection of email addresses, we can add any number of recipients. Each will receive a copy of the same message. To do so, add more than one MailAddress object to the property. NB: Each recipient will be able to see the other recipients' email addresses so this method should not be used if this information must be kept private.

MailMessage message = new MailMessage();
message.From = new MailAddress(SenderEmail, "Richard");
message.To.Add(new MailAddress(RecipientEmail1, "Lisa"));
message.To.Add(new MailAddress(RecipientEmail2, "Chris"));
message.Subject = "Hello";
message.Body = "Hi.\nHow are you?";
client.Send(message);

Adding a Reply To Address

Sometimes you will want to send an email from a specific email address but will want replies to the email to be received at an alternative address. In this case, you can add a "reply to" address using the ReplyTo property. The received email will show the same sender but replies will be redirected. The following example sends an email from Richard to Lisa but replies to the message will be sent to Chris.

MailMessage message = new MailMessage();
message.From = new MailAddress(SenderEmail, "Richard");
message.To.Add(new MailAddress(RecipientEmail1, "Lisa"));
message.ReplyTo = new MailAddress(RecipientEmail2, "Chris");
message.Subject = "Hello";
message.Body = "Hi.\nHow are you?";
client.Send(message);
21 April 2008