Later Today: ASP.NET Tutorial 2: Introduction to C#

(C#) Sending an Email (Plain-Text & HTML)

Prerequisites: Firm grasp of ASP.NET basics, including objects, instantiation, and string building.

This article covers the various methods of sending an email using C#, either in plain text or HTML.

This method of sending an email requires the use of the System.Net.Mail namespace. Simply place a namespace reference at the top of your class file with all the other using statements. The reference should look something like this:

(C#) Required Framework Namespace:
using System.Net.Mail;

> Email Basics in C#

This details the bare essentials for sending a plain-text email in C#/ASP.NET.

The code logic for sending an email in C# is this:

  1. Instantiate a new MailMessage object to use for building the email.
  2. Instantiate a new SmtpClient object to use for connecting to the mail server.
  3. Add mail recipients to the new email object using the email object’s .To.Add method.
  4. Set the email object’s From property (must be created using the MailAddress class).
  5. Set the email object’s Subject property (just a string).
  6. Set the email object’s Body property (just a string).
  7. Set the email object’s IsBodyHtml property (boolean).
  8. Use the SmtpClient object to send the email object.

As you can see, the logic is fairly straightforward. Sending a simple, plain-text email to two people would look like this:

(C#) Simple Plain-Text Email Example:
//Create the key email objects
MailMessage myemail = new MailMessage(); //Create message object
SmtpClient mysmtpserver = new SmtpClient("mail.example.com"); //Set mail server

//Set mail recipients
myemail.To.Add("user1@example.com");
myemail.To.Add("user2@example.com");

//Set email properties
myemail.From = new MailAddress("website@example.com", "Example.com Website");
myemail.Subject = "This is the Email Subject";
myemail.Body = "Hi there! This is the body of the email. I hope it's helpful!";
myemail.IsBodyHtml = false; //Send this as plain-text

//Send the email
mysmtpserver.Send(mail);

Note: The From property does not accept a simple string for an address, you must create a new MailAddress object containing both the email address and the display name. As in the above example, this can be done by simply setting the email object’s From property equal to new MailAddress("Email@Address.com","Display Name").

> Sending an HTML Email

This section builds upon previous examples by demonstrating how to build an an email with HTML code.

Sending an email with HTML is almost exactly the same as sending a plain-text email. You simply do two things:

  1. Set your mail object’s IsBodyHtml property to true.
  2. Include HTML code in your mail object’s Body string.

A very simple HTML email might look like this:

(C#) Simple HTML Email Example:
//Create the key email objects
MailMessage myemail = new MailMessage(); //Create message object
SmtpClient mysmtpserver = new SmtpClient("mail.example.com"); //Set mail server

//Set mail recipients
myemail.To.Add("user1@example.com");
myemail.To.Add("user2@example.com");

//Set email properties
myemail.From = new MailAddress("website@example.com", "Example.com Website");
myemail.Subject = "This is the Email Subject";
myemail.Body = "<h1>Email Notice!</h1><p>Hi there!</p><p>This is the body of the email. I hope it's helpful!</p><p>-Mr. Example </p>";
myemail.IsBodyHtml = true; //Send this as plain-text

//Send the email
mysmtpserver.Send(mail);

Note: you do NOT have to specify <html> or <body> tags in your email’s Body property. Setting the IsBodyHtml property to true is all that is required.

> Other Tips (CC, BCC, etc)

This section discusses other tips, including use of CC, BCC, and advanced formatting.

Tip: You can set CC & Bcc

The MailMessage class also contains properties for CC (Carbon Copy) and BCC (Blind Carbon Copy). These function identically to adding a recipient above. To add a CC or BCC to your email, simply add the following…

(C#) Adding CC or BCC to an Email Object:
myemail.CC.Add("user3@example.com");
myemail.Bcc.Add("user4@example.com");

Tip: Loop Through Recipient Addresses

Because recipient email address can be easily added to a mail object with To.Add(), CC.Add(), and Bcc.Add() – it is possible to loop through an array or collection of addresses to add recipients. This is an excellent way to dynamically build a list of recipients from data you’re pulling from elsewhere. Here’s an example of how this might be done from an array:

(C#) Use Array to Set Email Recipients:
//Create the array of addresses to contact
string[] strAddresses = {"user1@example.com", "user2@example.com", "user4@example.com",};

//Create the key email objects
MailMessage myemail = new MailMessage(); //Create message object
SmtpClient mysmtpserver = new SmtpClient("mail.example.com"); //Set mail server

//Set mail recipients dynamically
foreach (string strAddress in strAddresses)
{
   myemail.To.Add(strAddress);
}

//Set email properties
myemail.From = new MailAddress("website@example.com", "Example.com Website");
myemail.Subject = "This is the Email Subject";
myemail.Body = "<h1>Email Notice!</h1><p>Hi there!</p><p>This is the body of the email. I hope it's helpful!</p><p>-Mr. Example </p>";
myemail.IsBodyHtml = true; //Send this as plain-text

//Send the email
mysmtpserver.Send(mail);

No Comments

Post a Comment

You must be logged in to post a comment.

RSS Twitter LinkedIn Facebook
Doing neato things with JavaScript, please wait...