In this article, I am going to consider the process of managing and sending emails inside a Web application. Almost every Web site has a feedback form or a registration form. After validating user's entries and doing some actions, it is necessary sometimes to send a notification email to the user or, for instance, to a moderator of the site. To achieve this goal, you can use the standard .NET classes (read more here and here if you are using .NET 2.0). But how can you manage permanent content of emails inside your Web application?
The simplest way is to hardcode email content and insert dynamic values using concatenating functions inside page's code. But what if you need to change design of email or add some functionality to the page source? Email template can be very big and contain a pile of dynamic variables with concatenation correspondingly. Looking into such source is horrible. Everybody knows it and in spite of this, even professional programmers often choose the way to place content email in the source code.
Summarizing all problems, you can be faced with:
- Source of page that sends email is hardly readable because there are a lot of long string literals and a lot of concatenate operations
- You cannot preview email template until you actually run the code
- It's necessary to recompile the application after even a small content change in email template
The solution of the problem is simple – using email templates and parser. For example, we need to send email to a user after registration is completed. This email must contain such information like first and last name to say hello and in addition we need to send login information too. Example template can be similar to this one:
Hello ##FirstName## ##LastName##,
You have been registered with the following information:
To login use this link: www.server.com
##FirstName## in the body of the message does mean a marker of place in the template where parser inputs a value of
FirstName variable. So we have four places where we need to place personal information for each user. Other contents are permanent. Also please notice that we can use HTML as content for template instead of plain text in the previous example:
<p>Hello ##FirstName## ##LastName##,</p>
<p>You have been registered with the following information:</p><table>
</table><p>To login use this link:
To use this template in your code, we need to save it in a separated file. I called it as Registration.htm and placed it in MailTemplates folder where I can manage all application mail templates. Now we can use the registration template in code:
Dim templateVars As New Hashtable()
Dim parser As New Parser_
Dim message As New MailMessage()
message.From = New MailAddress("<a href="mailto:email@example.com">firstname.lastname@example.org</a>")
message.To.Add(New MailAddress("<a href="mailto:email@example.com">firstname.lastname@example.org</a>"))
message.Subject = "Registration"
message.BodyEncoding = System.Text.Encoding.UTF8
message.Body = parser.Parse
message.IsBodyHtml = True
Dim client As New SmtpClient()
Label1.Text = "Email has been sent."
Catch ex As Exception
Label1.Text = "Could not send email: " + ex.Message
The first key element of sending code is creating
templateVars object as instance of
Hashtable class from
System.Collections and pushing all mentioned variables in the template with key equal variable name. After that, we can create a parser object as an instance of
Parser class passing template path and hashtable with template variables as parameters to the constructor. To get a template with replaced values, you can simply invoke the
Parse method of the
To configure SMTP server settings, add <system.net />configuration section in web.config file:
<network defaultCredentials="true" host="localhost" port="25" />
For more information, read this MSDN article.
Now let's imagine you need to replace all unsafe HTML special chars with HTML-encoded equivalents to prevent a user from using tags in first name or somewhere and destroy view of HTML email message. Of course, you can use
Server.HTMLEncode() method and rewrite adding variables in the hashtable:
But after that, we need to recompile the application. And it will hang up application performance if we need to make it on the server directly. Another approach is using value modificators – simple functions which have variable value as one of the parameters. For example:
<p>Hello ##FirstName:HTMLENCODE## ##LastName:HTMLENCODE##,</p>
Now if the user inputs "
<b>Alex</b>" as first name, the value actually will be inserted in the template as "
<b>Alex</b>". And we do not need to change and recompile application sources. In the table below, you can find all built-in value modificators with a short functional description:
|Replace all new line chars with |
<br /> tag.
|Change all unsafe HTML special chars with HTML-encoded equivalents.|
string to uppercase.
string to lowercase.
|Remove all spaces from right and left side.|
|Remove spaces from end only.|
|Remove spaces from start only.|
If it is not enough for your claims, you can create custom value modificators. For instance, if you need to create a modificator to change only the first letter to upper (to represent names) you can create such a class:
Public Overrides Sub Apply(ByRef Value As String, _
ByVal ParamArray Parameters() As String)
Value = Value.ToUpper()(0) + Value.ToLower().Substring(1)
Compile it in assembly and place in the bin folder. Using Reflection
Parser object will determine that you declared a new custom
FIRSTUPPER modificator and will use it as soon as it finds a link to it from the template.
Next I am going to explain how to use conditions in email templates. There can be such a situation when depending on some variable value we need to use one or the other block of template. A simple example is show "Please sign in" message if user is not logged into a system or show greeting with full name in other case:
<p>Hello, ##FirstName## ##LastName##!</p>
Please sign in.
IsRegisteredUser must contain a boolean variable which determines user login status. Please notice also that you can use variables
LastName in conditional blocks.
Blocks and Loops
Finally, I want to show how to display a list of data in template using Block declaration. You can be faced with a problem when you need to place some list of data in a template which rows you need to generate in code. For example - list of links like this one:
The problem is we do not know how many links we have but it's clear that we can declare iterative block and use it in a loop. In the following situation, such block is
<li> tag and everything that it includes. So we can put it in a
Block as "Link" and replaced two areas with
##Title## variables. Now I can iterate through Hashtable with links data and create template block with a list of all links:
Dim links As New Hashtable()
Dim linksBlock As New StringBuilder()
Dim myEnumerator As IDictionaryEnumerator = links.GetEnumerator()
Dim blockVars As New Hashtable()
To get parsed content of each link, I use the
ParseBlock() method. The first parameter is the name of the
Block and the second is
Hashtable of variables which is needed to replace in the current
Using template parser, you can simply store and manage all application email templates in one place. This approach helps separate content from code sources and gives you the ability to edit them easily.
- 16/8/2006 - Some fixes - Added ability to declare and use Blocks and use them to create a list of data. Convert sources of .NET 2.0 version to VB.NET.
- 7/29/2007 - Added sources for .NET 1.1.