If you find this article useful, consider making a small donation to show your support for this web site and its content.
Free app Developer Interview available here.

Available on the iPhone App Store
Available on the Google Play
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.



Email Template using C#

by Farooq Kaiser 27. January 2010 03:46

In this article, i will examined how to build template to store email data such as body and subject. The template is an XML file that will store the static text with dynamic Tags. The dynamic tags are just like variables that get replaced by the real data. Here is a sample template.

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <email>
   3:  
   4:   <subject>Your order</subject>
   5:  
   6:   <body> 
   7:  
   8:   Dear ##FirstName## ##LastName##,
   9:  
  10:   <br /><br />
  11:   Please review your order below:
  12:  
  13:   <b>Description:</b> ##Description##<br />
  14:  
  15:   <b>Order Number:</b> ##OrderNumber##<br />
  16:  
  17:   <b>Quantity:</b> ##Quantity##<br />
  18:  
  19:   <b>Price:</b>  ##Price##<br />
  20:  
  21:   <b>Your Total:</b> ##Total##<br />
  22:    <br /><br />
  23:  
  24:   Sincerely, <br />
  25:  
  26:   ##Signature##    
  27:   
  28:   </body>
  29: </email>

Now, i will load the template in C# and Tags will be replaced with Real Data. Here is C# sample code.

   1: XmlDocument xdox = new XmlDocument();
   2: xdox.Load(Template);  // Load your template here
   3: string Subject = xdox.SelectSingleNode("//subject").InnerText;
   4: string Body = xdox.SelectSingleNode("//body").InnerText;
   5:  
   6: // Here I will create a dictionary collection that will hold template tags and their values.
   7:  
   8: Dictionary<string, string> Dic = new Dictionary<string, string>();
   9:  
  10: Dic.Add("FirstName", "Farooq");
  11: Dic.Add("LastName", "Kaiser");
  12: Dic.Add("Description", "Ipod");
  13: Dic.Add("OrderNumber", "543535");
  14: Dic.Add("Quantity", "1");
  15: Dic.Add("Price", "$100");
  16: Dic.Add("Total", "$100");
  17: Dic.Add("Signature", "Store Manager");
  18:  
  19: // Now, i will use regular expression to replace the values in a template.
  20:  
  21:     string HashTagPattern = @"##([A-Za-z0-9]+)##"; 
  22:  
  23:     foreach (Match match in Regex.Matches(Body , HashTagPattern))
  24:      {
  25:         if (match.Success)
  26:         {
  27:           try
  28:            {
  29:              Body = Body.Replace(match.Value, dic[match.Value.Replace("##", "").Trim()]);
  30:            }
  31:          catch { }
  32:         }
  33:      }

Now, we have a Subject and Body ready, Here is our email code.

   1: MailMessage MMsg = new MailMessage();
   2: MMsg.IsBodyHtml = true;
   3: MMsg.From = new MailAddress("you@domain.com");
   4: MMsg.To.Add(Customer@domain.com);
   5: MMsg.Subject = Subject;
   6: MMsg.Body = Body;
   7:  
   8: SmtpClient client = new SmtpClient();
   9: client.Send(MMsg);

 

In web.config, we will configure the port number and host name.

   1: <configuration>
   2:   <system.net>
   3:     <mailSettings>
   4:       <smtp>
   5:         <network
   6:              host="mail.server.com"
   7:              port="25" />
   8:       </smtp>
   9:     </mailSettings>
  10:   </system.net>
  11: </configuration>

Summary

In this article, we examined how to build template based email.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

.NET | C# | XML


comments powered by Disqus

Comments

Jobs Autos Real estate Videos Power by Google