Quick Tip: Use ASP.NET Templating To Generate E-mail Bodies Or Other Templated Things

by Jon Davis 10. October 2008 22:38

I've been in at least three jobs now where a significant amount of effort by others on the team was spent performing e-mail blasts to thousands or tens of thousands of [opted-in] users, whether for newsletters or for e-mail advertising campaigns. And I've also had countless encounters of requirements of a web site to send an e-mail to a single user, such as upon registration or user-to-user communications via web-based PM interfaces.

It has been surprising to me to discover how insistent development teams are with using alternative templating techniques to generate the pre-formatted e-mails, everything from:

  • .txt files with variable markers like {{USERNAME}}
  • NVelocity templates
  • Database records with text BLOBs and variable markers like %%USERNAME%%
  • C#-written code with lots and lots of myStringBuilder.Append()'s.

(I do like the database text BLOBs if only for simple plain-text formatted e-mails, as this sort of thing allows non-developer system administrators to customize their templates on the fly.)

Yet, never do people take advantage of the templating system they're already using. It's called ASP.NET.

Why would you use ASP.NET? Actually, in a web context, why wouldn't you might be the fairer question, because reasons for using it are obvious:

  • ASP.NET is one of the most powerful and versatile templating engines in use today. Why anyone would use NVelocity in a world where there's ASP.NET is beyond me.
  • You can take advantage of the full .NET Framework, web services, HttpContext features, and more.
  • You can data-bind to the database and output the same user-customized Rich HTML as you could with normal web pages.
  • You can add repeaters and other multi-dimensional variables that are not possible with flat templates.


  • Requires the use of an insanely powerful service that is likely already running to support such things. (That would be IIS.)

Say, for instance, you're invoking it from a web page that has just processed some database transaction and you need to e-mail the user to inform him of a status or reminder or something. You can use Server.Execute() to invoke the ASP.NET template, which will read the results back into a Stream object. Pass a MemoryStream into it as a parameter, then flush that out into a StreamReader to read it into a System.String. Easy! What about context and state? All the more reason to use Server.Execute and not System.Net.WebClient. You can retain state, although in any case you can also build up a lengthy query string in your URL to be invoked, and then on the template you can perform whatever context synchronization that needs to be performed.

Here's a sample that demonstrates how (but not why).

EDIT: For an updated ASP.NET MVC Razor example, check out this gist: https://gist.github.com/stimpy77/a49bad26b9fca299332a



	<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %> 
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head runat="server">
	<form id="form1" runat="server">
	<%=TemplateOutput %>


	protected string TemplateOutput
	MemoryStream ms = new MemoryStream();
	StreamWriter sw = new StreamWriter(ms);
	Context.Items["Question"] =
	"Is this a working template?";
	Server.Execute("Template.aspx", sw, true);
	ms.Position = 0;
	StreamReader sr = new StreamReader(ms);
	return sr.ReadToEnd();


	<%@ Page Language="C#" %>
	<%= Context.Items["Question"].ToString()
	.Replace("Is this", "This is").Replace("?", "!") %> 


This is a working template!

I hope this was helpful.

kick it on DotNetKicks.com

Be the first to rate this post

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

Tags: , ,

ASP.NET | Web Development


Add comment

(Will show your Gravatar icon)  

  Country flag

  • Comment
  • Preview


Powered by BlogEngine.NET
Theme by Mads Kristensen

About the author

Jon Davis (aka "stimpy77") has been a programmer, developer, and consultant for web and Windows software solutions professionally since 1997, with experience ranging from OS and hardware support to DHTML programming to IIS/ASP web apps to Java network programming to Visual Basic applications to C# desktop apps.
Software in all forms is also his sole hobby, whether playing PC games or tinkering with programming them. "I was playing Defender on the Commodore 64," he reminisces, "when I decided at the age of 12 or so that I want to be a computer programmer when I grow up."

Jon was previously employed as a senior .NET developer at a very well-known Internet services company whom you're more likely than not to have directly done business with. However, this blog and all of jondavis.net have no affiliation with, and are not representative of, his former employer in any way.

Contact Me 

Tag cloud


<<  June 2021  >>

View posts in large calendar