Home » none » Templated E-Mail using .NET

Templated E-Mail using .NET

G03A0053

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);

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Test Sending Email without a Server in ASP.NETTest Sending Email without a Server in ASP.NET By now, most people are familiar with the fact that ASP.NET will send mail from the codebehind by simply adding a few lines to your web.config file and adding another few lines of code in […]
  • DotNetNuke Modules – Creating Base ModulesDotNetNuke Modules – Creating Base Modules Now that we have DotNetNuke installed into Visual Studio we can go ahead and create our first modules. Actually, creating the modules is pretty simple. But it is even easier to do it […]
  • ASP.NET Session Variables Not StickingASP.NET Session Variables Not Sticking I’ve stumbled across this problem twice in the last couple of months so I figure it is about time I blogged about it. The situation is that you have  a page on your web site that […]
  • Managing EmailManaging Email I recently started using a product called Clear Context that does a much better job of integrating Outlook's email with Outlook's calendar. The two things I like best about it are that it […]
  • JQuery, Cufon, and Dynamic ContentJQuery, Cufon, and Dynamic Content Yesterday I mentioned a new tool called Cufon that allows you to easily embed fonts in your web site. One quirk about this tool is that your HTML has to be rendered prior to applying, […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

One Pingback/Trackback

  • http://alekdavis.blogspot.com Alek Davis

    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.

    • Dave

      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.

  • http://alekdavis.blogspot.com Alek Davis

    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).

  • http://www.sistemi.hr Tonci

    Ader TemplateEngine if you need a little more power and flexibility in your email templates (http://www.1000ad.net/index.cfm?page=templateEngine2)

  • Pingback: Dew Drop – August 25, 2009 | Alvin Ashcraft's Morning Dew()

  • http://www.bubriski.com John Bubriski

    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!

    • Dave

      That’s an interesting mix of solutions.

      The stringwriter with the ascx control looks the most promising to me.

  • http://www.shbabikna.com/vb jon

    That’s an interesting mix of solutions

  • http://alekdavis.blogspot.com Alek Davis

    Okay, here is my approach for more complex templates: http://alekdavis.blogspot.com/2009/08/sending-html-based-email-from-net.html