Click here to Skip to main content
13,193,048 members (74,393 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 19 Jun 2012

An SMTP email client with dynamic template based email engine

, 21 Jun 2012
Rate this:
Please Sign up or sign in to vote.
A simple SMTP mail client with xml based template factory imlementation


This article creates a reusable email client which has an XML based template email service. Particular texts in the email body can be dynamically modified.

The major components in the article are:

  1. A simple SMTP mail server 
  2. XML based template to dynamically select email body and header icon(optional). 
  3. Dynamically replacing the particular entries in the Email Body. This functionality is XML driven. 
  4. XML serialization using XSD and designer class. 


For an application it might be required to send multiple mails to the user at a certain stage of execution in the application. We also require to dynamically format the email, e.g., the addressee. So here we have tried to create an email templating engine driven by XML. We can get the dynamic data from any source. It can be a database or another XML source.

We can set the required information like the To/From email addresses to the class and mention a template. Rest will be taken care by the engine.

Using the code

The namespaces which needs to be added are:

using System.Net.Mail;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.IO;
using System.Xml.Serialization;

The "MailInfo" class is used to hold the basic data required for sending an email.

class MailInfo
    public string ToAddress { get; set; }
    public string FromAddress { get; set; }
    public string Subject { get; set; }
    public  MailInfo(string toAdd, string fromAdd, string subject)
        ToAddress = toAdd;
        FromAddress = fromAdd;
        Subject = subject;
    public MailInfo()

The MailType enum is the driving factor for email template engine. This is basically a vocab for all possible email types.

enum MailType

The SMTPEmailClient class is the parent class responsible for creating and sending an email.

It holds a SmtpClient object as member variable. This is used for sending the messages. 

private SmtpClient smtpServer = null; 



This method is used to initialize the SMTP server.

public void SetSMTPServerInfo(string ipAddresss,int portnumber =25,bool isSecured =false)
        smtpServer = new SmtpClient(ipAddresss);
        smtpServer.Port = portnumber;
        smtpServer.EnableSsl = isSecured;//by default this is false
    catch (Exception)


This method returns a ListDictionary which is used with MailMessage object to dynamically update the email content. 

private ListDictionary GetReplacements(List<EmailTemplatesEmailInput> list)
    ListDictionary replacements = null;
        replacements = new ListDictionary();
        foreach (EmailTemplatesEmailInput item in list)
            replacements.Add("<%" + item.type + "%>", GetDataFromType(item.type));
    catch (Exception)
     return replacements; 


This method's job is to retrieve dynamic data from any data source depending on XML tags. As of now the sample code returns hardcoded data: 

private string GetDataFromType(string type)

Finally the main entry point method is SendEmail.


This method takes MailInfo  and MailType as parameter.

It does following actions sequentially:

  1. Gets mail related template info from an XML file using serialization
  2. Retrieves the template as per MailType.
  3. Next it creates a MailMessage and MailDefinition and update the structures as per input. 
  4. Using CreateMailMessage method present in MailDefinition we format the email body .
  5. Finally we create AlternateViews for Text and Icon and add them to the MailMessage.
  6. Send the email using smtpServer.Send(msg)
public bool SendEmail(MailInfo info, MailType type)

The template File.

As of now the template file is XML as per the following schema:

<?xml version="1.0" encoding="utf-8" ?>
  <Email type ="Success">
    <!-- the filaname gives the name of the file-->
    <!--the HeaderIcon is an optional tag-->
    <!--Content needs to be removed-->
    <!--The inputList contains the tags which needs to be replaced in the body-->
      <Input type= "Name"/>
      <Input type= "Source"/>
  <Email type ="Failure">
      <Input type= "NickName"/>
      <Input type= "Test"/>

The EmailTemplates class is auto-generated using XSDToCode.

As of now all the template files needs to be present in current directory

//header logo file
//Temple files with html tag
//Templates file
//Schema file for deserialization

Points of Interest

Well, always wanted to learn how SMTP works in dot net, Also I wanted to make it as generic as possible. Still lot of work to be done on this. Hope this will help others to come up with an email client.


 Please rate the article. Help me to improve.  


  • 18/06/12: First created. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionRazor engine Pin
box14021-Jun-12 20:16
memberbox14021-Jun-12 20:16 
AnswerRe: Razor engine Pin
sayon sur22-Jun-12 7:30
membersayon sur22-Jun-12 7:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171017.2 | Last Updated 21 Jun 2012
Article Copyright 2012 by dreamzlife
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid