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.

Adding Carbon Copies and Blind Carbon Copies

When sending an email that is intended for one or more primary recipients, you may also want to send a copy to other, secondary recipients who would be interested in the message contents. To indicate that the email is not directly for those secondary people, they can be added as carbon copy (CC) recipients. These are visible to all recipients of the message.

As an alternative, email addresses can be added to the blind carbon copy (BCC) list. People on this list receive the email as expected but their details are hidden from other recipients.

The following code sends an email and includes a CC and BCC address using the appropriately named "CC" and "Bcc" properties of the MailMessage object. Note that both lists accept multiple addresses using a MailAddressCollection.

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

Adding Formatting to an Email

If you wish to send emails that include font and paragraph formatting, hyperlinks or images, you can include HTML tags in the message body. To enable HTML, the IsBodyHtml property must be set to true. If this property is false, the message and tags will be sent in plain text.

The following sample sends a HTML version of the message as we have used previously. In this case, the "\n" control character is replaced with the break tag (<br>) and the first line of the message is highlighted with bold text.

MailMessage message = new MailMessage();
message.From = new MailAddress(SenderEmail);
message.To.Add(new MailAddress(RecipientEmail1));
message.Subject = "Hello";
message.IsBodyHtml = true;
message.Body = "<html><body><b>Hi.</b><br>How are you?</body></html>";
client.Send(message);

Setting an Email's Priority

By default, email messages are sent with normal priority. The importance of the email can be changed using the Priority property of the MailMessage class. This property accepts a value from the MailPriority enumeration. Possible values are High, Normal and Low.

The following code sends a high priority mail message:

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

Adding Attachments to an Email

One of the most useful elements of email is the option to send file attachments. The MailMessage class permits the addition of multiple attachments via the Attachments property. This property contains a collection, to which objects of the Attachment class may be added.

The Attachment class can be used to hold file or stream data. The simplest way to create an attachment is to pass the path and name of a file to attach as a string parameter of the class's constructor. This process is used in the example below to attach the "win.ini" file to the email. NB: Ensure that this file exists before running the code.

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

Attachment attachFile = new Attachment(@"c:\windows\win.ini");
message.Attachments.Add(attachFile);

client.Send(message);
21 April 2008