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.
- private void SendEmail()
- {
- Customer customer = CustomerData.GetCustomer(2);
- MailDefinition mailDefinition =new MailDefinition();
- mailDefinition.BodyFileName = "~/Email-Templates/Order-Confirmation.html";
- mailDefinition.From ="[email protected]";
- //Create a key-value collection of all the tokens you want to replace in your template...
- ListDictionary ldReplacements = new ListDictionary();
- ldReplacements.Add("<%FirstName%>", customer.FirstName);
- ldReplacements.Add("<%LastName%>", customer.LastName);
- ldReplacements.Add("<%Address1%>", customer.Address1);
- ldReplacements.Add("<%Address2%>", customer.Address2);
- ldReplacements.Add("<%City%>", customer.City);
- ldReplacements.Add("<%State%>", customer.State);
- ldReplacements.Add("<%Zip%>", customer.Zip);
- string mailTo = string.Format("{0} {1} <{2}>", customer.FirstName, customer.LastName, customer.EmailAddress);
- MailMessage mailMessage = mailDefinition.CreateMailMessage(mailTo, ldReplacements,this);
- mailMessage.From =new MailAddress("[email protected]","test site");
- mailMessage.IsBodyHtml = true;
- mailMessage.Subject ="Order Details";
- SmtpClient smtpClient = new SmtpClient(ConfigurationManager.AppSettings["SMTPServer"].ToString(), 25);
- smtpClient.Send(mailMessage);
- }

GianfrancoPosted Apr 3, 2019, 4:26 AM
Not work :( Nothing files
Syrym SatanovPosted Nov 18, 2015, 4:40 PM
Download file is not there!
Diego QuesadaPosted Sep 27, 2014, 6:18 PM
Hi, download files are not available
Ramakrishna PathuriPosted Aug 31, 2013, 7:22 AM
Are You looking For Code ...@chandan bisht
Ramakrishna PathuriPosted Aug 29, 2013, 1:54 PM
I was uploaded files check once...
Chandan BishtPosted Aug 29, 2013, 11:30 AM
Hello ,I Am Not able to download the source code ! plz Help ............... Thanks & regards chandan
Ramakrishna PathuriPosted Aug 29, 2013, 11:28 AM
I will always follow ...thank you very much...
Mahesh ChandPosted Aug 29, 2013, 8:52 AM
Interesting. I didn't know about MailDefinition. I guess you learn something new everyday :). Thank you for sharing Rama.
Dinesh BeniwalPosted Aug 29, 2013, 8:42 AM
Good start, Welcome to the C# Corner Ramakrishna
Joxin StanlyPosted Aug 29, 2013, 8:16 AM
Thank you for sharing this info about MailDefinition