Send Templated Emails Using MailDefinition Object in ASP.Net

I recently found a better way to format my email messages in ASP.NET; using the MailDefinition object. Lets you use an email template and define tokens that you want to replace in it. This helps keep the presentation and business layers clean & separate and lets the designers go in and edit the email templates without having to navigate the StringBuilder. I think that any modern piece of software today, needs to send e-mail. Whether it being password recovery e-mails, rich reports, newsletters or anything else, being able to easily see and customize the look and feel of your e-mails is vital.

This article explains how to send email using ASP.NET. Yes, there are many other articles that cover sending email via .NET, I found that many articles suggest you create your HTML email using a string with the HTML markup in it. In this article we will look at a more detailed solution. One in which we use a regular HTML file as our template for the email. The template file will be a standard HTML file with the exception of some placeholders that we will use to populate our content and images right before we send the email. Think mail-merge in Microsoft Word. Finally, we will also learn how to send the email in such a way that if the email recipient's mail-client can't render HTML then they will get an alternate plain text version.

This is how it's done.

The following is the SendEmail method: 
  1. private void SendEmail()  
  2. {  
  3.     Customer customer = CustomerData.GetCustomer(2);  
  4.     MailDefinition mailDefinition =new MailDefinition();  
  5.     mailDefinition.BodyFileName = "~/Email-Templates/Order-Confirmation.html";  
  6.     mailDefinition.From ="";  
  7.     //Create a key-value collection of all the tokens you want to replace in your template...  
  8.     ListDictionary ldReplacements = new ListDictionary();  
  9.     ldReplacements.Add("<%FirstName%>", customer.FirstName);  
  10.     ldReplacements.Add("<%LastName%>", customer.LastName);  
  11.     ldReplacements.Add("<%Address1%>", customer.Address1);  
  12.     ldReplacements.Add("<%Address2%>", customer.Address2);  
  13.     ldReplacements.Add("<%City%>", customer.City);  
  14.     ldReplacements.Add("<%State%>", customer.State);  
  15.     ldReplacements.Add("<%Zip%>", customer.Zip);  
  16.     string mailTo = string.Format("{0} {1} <{2}>", customer.FirstName, customer.LastName, customer.EmailAddress);  
  17.     MailMessage mailMessage = mailDefinition.CreateMailMessage(mailTo, ldReplacements,this);  
  18.     mailMessage.From =new MailAddress("","test site");  
  19.     mailMessage.IsBodyHtml = true;  
  20.     mailMessage.Subject ="Order Details";  
  21.     SmtpClient smtpClient = new SmtpClient(ConfigurationManager.AppSettings["SMTPServer"].ToString(), 25);  
  22.     smtpClient.Send(mailMessage);  
  23. }
Your email template could be of any extension (txt, html, and so on) as long as its in a text format. I personally like to keep it in HTML format so that we can preview the email template in a browser. Basically it'll looks something like this:
Hello <%FirstName%> <%LastName%>,
Thank you for creating an account with us. Here are your details:
<%City%>, <%State%> <%Zip%>
Thank You,
My Site 
Change the web.config file mail settings as  in the following  for setting mail host details:

  1. <>  
  2.      <mailSettings>  
  3.          <smtpfrom="">  
  4.              <networkhost="" =""/>      
  5.     </smtp>         
  6.   </mailSettings>  
  7. </>


The MailDefinition class can be used by controls to create a MailMessage object from a text file or a string that contains the body of the e-mail message. Use the MailDefinitionclass to simplify creating predefined e-mail messages to be sent by a control. If you want to send e-mail not using a control, see the System.Net.mail class.

You can make text substitutions in the body of the e-mail message by passing to the CreateMailMessage method an IDictionary instance that maps strings to their replacements.

Small Note :

The MailDefinition class does not support data binding. Properties of the MailDefinition class cannot be bound to data using the <%#   %> data-binding expression syntax.