Email Templates in ASP.NET
Till recently, whenever it was required to send an email, through a web application using SMTP, I would go ahead and use SMTPClient, a class provided by .NET Framework.All I needed to do for this, is to include namespace System.Net.Mail and I am ready to go.I would read the SMTP host IP, port number and timeout value from the configuration file and write a code snippet like below:
String hostIP=Convert.ToString(ConfigurationManager.AppSettings["SMTPHostIP"]);
Int16 portNumber = Convert.ToInt16(ConfigurationManager.AppSettings["SMTPPort"]);
Int32 timeout = Convert.ToInt32(ConfigurationManager.AppSettings["SMTPTimeout"]);
SmtpClient smtpClient = new SmtpClient();
smtpClient.Timeout = timeout;
smtpClient.Host =hostIP;
smtpClient.Port = portNumber;
My next steps are to instantiate MailMessage class provided under the same namespace and invoke Send method, as below:
MailMessage mailMessage = new MailMessage
(fromAddress
,toAddress
,subject
,messageText);
smtpClient.Send(mailMessage);
Now, if the message text is a simple text, the above works fine.But, often we need to build the message programitically by concatenating data with the mail text.One way is to use a StringBuilder class and build the message text.There is yet another way which can simplify the code.Using email templates provided by ASP.NET.
We can build mail templates using a class MailDefinition.MailDefinition class is provided under namespace System.Web.UI.WebControls.
MailDefinition mailTemplate = new MailDefinition();
mailTemplate.BodyFileName = "~/mail_templates/TransactionConfirmation.html";
TransactionConfirmation.html is an html file which defines the structure of the email message.To insert data into the mail,we need to create a dictionary object like below:
DictionarymailData = new Dictionary();
mailData.Add("<% TransactionNumber%>", trans.TransactionNumber.ToString());
mailData.Add("<%CardNumber%>", card.CardNumber.ToString());
mailData.Add("<%BankName%>", card.BankName);
mailData.Add("<%CardHoldersName%>", card.CardHoldersName);
Next step is to create mail message.
MailMessage mailMessage = mailTemplate.CreateMailMessage
(toAddress,mailData,new LiteralControl());
mailMessage.IsBodyHtml = true;
mailMessage.Subject = subject
The third parameter in CreateMailMessage method above is the owner i.e. a System.Web.UI.Control that own this mail definition.An HTML Template can be defined as below:
<%trans.TransactionNumber%></span> </td> </tr> <tr> <td> <%card.CardHolderName%>,<br><br> This email confirms your transaction.
String hostIP=Convert.ToString(ConfigurationManager.AppSettings["SMTPHostIP"]);
Int16 portNumber = Convert.ToInt16(ConfigurationManager.AppSettings["SMTPPort"]);
Int32 timeout = Convert.ToInt32(ConfigurationManager.AppSettings["SMTPTimeout"]);
SmtpClient smtpClient = new SmtpClient();
smtpClient.Timeout = timeout;
smtpClient.Host =hostIP;
smtpClient.Port = portNumber;
My next steps are to instantiate MailMessage class provided under the same namespace and invoke Send method, as below:
MailMessage mailMessage = new MailMessage
(fromAddress
,toAddress
,subject
,messageText);
smtpClient.Send(mailMessage);
Now, if the message text is a simple text, the above works fine.But, often we need to build the message programitically by concatenating data with the mail text.One way is to use a StringBuilder class and build the message text.There is yet another way which can simplify the code.Using email templates provided by ASP.NET.
We can build mail templates using a class MailDefinition.MailDefinition class is provided under namespace System.Web.UI.WebControls.
MailDefinition mailTemplate = new MailDefinition();
mailTemplate.BodyFileName = "~/mail_templates/TransactionConfirmation.html";
TransactionConfirmation.html is an html file which defines the structure of the email message.To insert data into the mail,we need to create a dictionary object like below:
Dictionary
mailData.Add("<% TransactionNumber%>", trans.TransactionNumber.ToString());
mailData.Add("<%CardNumber%>", card.CardNumber.ToString());
mailData.Add("<%BankName%>", card.BankName);
mailData.Add("<%CardHoldersName%>", card.CardHoldersName);
Next step is to create mail message.
MailMessage mailMessage = mailTemplate.CreateMailMessage
(toAddress,mailData,new LiteralControl());
mailMessage.IsBodyHtml = true;
mailMessage.Subject = subject
The third parameter in CreateMailMessage method above is the owner i.e. a System.Web.UI.Control that own this mail definition.An HTML Template can be defined as below:
<%trans.TransactionNumber%></span> </td> </tr> <tr> <td> <%card.CardHolderName%>,<br><br> This email confirms your transaction.
<html><head>
<title>Transaction Confirmation</title>
</head>
<body>
<table><tr><td align="center">
<span>Transaction Confirmation for
Following are the details:<br><br> Card Number : <%card.CardNumber%><br> Bank Name : <%card.BankName%><br> </td> </tr> <tr> <td>Thank You </td> </tr> </table> </body> </html> Now isn't that cool.Happy Coding...
Comments