Templated E-Mail using .NET

One thing I’m pretty consistent about is letting the computer do most of my work for me. As a “programmer” I really don’t like to program. I prefer to solve problems.
You’ve already seen the effects of this in how I program PDF files where I use form fields and fill them at runtime rather than building up the entire PDF at runtime.
I use a similar technique when creating e-mails to send out from ASP.NET.
Here’s what I do
The first thing I do is to create an HTML file in my designer that looks just like what I want it to look like when the recipient receives it.
If you are planning to include images, you’ll want to make sure you include the base tag so that you can keep the images and the CSS on the server but build the page as though it lived on the web site instead of inside of a email client. Otherwise, you’ll want to embed any CSS in your HTML file.
Within the document, you place “tags” that indicate where you want content from your program to go. I normally use the format |tagName| where “tagName” is the name of the form field the content is coming from, if that is possible. For example, if I had a form field I had named m_textBoxFirstName, my tag would be |m_textBoxFirstName|. This makes my code easy to maintain in the future.
Once I’ve created my template, I just need a bit of code that will load the template and replace the variables.
StreamReader htmlStream = null; htmlStream = new StreamReader(Server.MapPath("template.html")); string htmlString = htmlStream.ReadToEnd(); htmlStream.Close(); htmlStream.Dispose(); // multiple replace lines htmlString = htmlString.Replace("|m_textBoxFirstName|", m_textBoxFirstName.Text); // send the email SmtpClient smtp = new SmtpClient(); MailAddress from = new MailAddress(fromEmail, fromEmail); MailAddress to = new MailAddress(emailAddress, emailAddress); MailMessage message = MailMessage(from, to); message.Subject = SubjectLine; message.Body = htmlString; message.IsBodyHtml = true; smtp.Send(message);
Other Related Items:
Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition (Windows.Net)The first edition of this book was released at the 2001 Tech Ed conference in Atlanta, Georgia. Since that time, this text has been revised, tweaked, ... Read More >
200 Digital Photography Backdrops Urban Background Templates f200 Digital Photography Urban Portrait Studio Backgrounds from The Photo Coach
This collection of 200 urban digital photography backdrops is o... Read More >









This method would be fine for simple e-mails with one or two single-word placeholders, but for anything more complex, a more flexible option would be to use XSL templates.
Actually, this method works quite well with quite a few fields.
I’d love to see your XSLT solution, let us know when you’ve written about it.
I assumed that using XSLT for email templates is a common knowledge, but after a quick Google search I did not find any good examples, so I may follow your suggestion and write up a quick post on this. In the nutshell, the idea is to create an XML document (in code) with info that would be put in the XSL template, load the template and substitute placeholders in XSLT with data from XML (substitution is done automatically). This method is especially handy when you have to adjust text of the message based on the data you have and when you have variable length data collections (e.g. list of subscriptions).
Ader TemplateEngine if you need a little more power and flexibility in your email templates (http://www.1000ad.net/index.cfm?page=templateEngine2)
[...] Templated E-Mail using .NET (Dave M. Bush) [...]
I asked a question about this on StackOverflow: http://stackoverflow.com/questions/620265/can-i-set-up-html-email-templates-in-c-on-asp-net
Check it out, and feel free to contribute your information there too!
That’s an interesting mix of solutions.
The stringwriter with the ascx control looks the most promising to me.
That’s an interesting mix of solutions
Okay, here is my approach for more complex templates: http://alekdavis.blogspot.com/2009/08/sending-html-based-email-from-net.html