Email template engine

Newest version of Mail.dll email library for .NET includes easy to use template engine.

It allows you to easily create html template for your emails:

Loading and rendering such template requires one line of code:

// C# version

Contact templateData = ...;

 string html = Template
     .DataFrom(templateData )
' VB.NET version

Dim templateData As Contact = ....

Dim html As String = Template _
     .FromFile("template.txt") _
     .DataFrom(templateData) _

This is how the template looks like:

    <title>Your order</title>
	Hi [FirstName] [LastName],
	<br />
	Your account [if Verified]has[else]<strong>has not</strong>[end] been verified. <br/>
	Your password is: [Password].
	Here are your orders:
	[foreach Orders]
		Order sent to <strong>[Street]</strong>:
		<table style="width: 30%;">
			[foreach Items]
				<tr style="background-color: #E0ECFF;">
		Thank you for your orders.

Here’s the sample code that loads, fills the template and sends it using Mail.dll:

// C# version

string rendered = Template

MailBuilder builder = new MailBuilder();
builder.Html = rendered;
builder.Subject = "Your order";
builder.From.Add(new MailBox("", "Alice"));
builder.To.Add(new MailBox("", "Bob"));
IMail email = builder.Create();

using (Smtp client = new Smtp())
    client.UseBestLogin("", "password");
// C# fluent interface version

using Fluent = Limilabs.Mail.Fluent;

    .From(new MailBox("", "Alice"))
    .To(new MailBox("", "Bob"))
    .Subject("Your order")
    .WithCredentials("", "password")
' VB.NET version

    Dim rendered As String = Template _
        .FromFile("template.txt") _
        .DataFrom(templateData) _

    Dim builder As MailBuilder = New MailBuilder()
    builder.Html = rendered
    builder.Subject = "Your order"
    builder.From.Add(New MailBox("", "Alice"))
    builder.[To].Add(New MailBox("", "Bob"))
    Dim email As IMail = builder.Create()

    Using client As Smtp = New Smtp()
        client.UseBestLogin("", "password")
    End Using
' VB.NET fluent interface version

Imports Fluent = Limilabs.Mail.Fluent;

Fluent.Mail.Html(Template _
               .FromFile("template.txt") _
               .DataFrom(templateData) _
               .Render()) _
     .From(New MailBox("", "Alice")) _
     .[To](New MailBox("", "Bob")) _
     .Subject("Your order") _
     .UsingNewSmtp() _
     .WithCredentials("", "password") _
     .Server("") _
     .WithSSL() _

You can also specify text version of the message, by using Text(string) method. In most cases this is unnecessary, as Mail.dll automatically extracts plain text from html and adds the plain text version to the email message.

And this is how the data (templateData variable), used by the template, look like:

// C# version

public class Contact
    public List<order> Orders { get; private set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool Verified;
    private string Password { get; set; }

    public Contact()
        Orders = new List<order>();
} ;
' VB.NET version

Public Class Contact
	Public Property Orders() As List(Of Order)
			Return m_Orders
		End Get
		Private Set
			m_Orders = Value
		End Set
	End Property
	Private m_Orders As List(Of Order)

	Public Property FirstName() As String
			Return m_FirstName
		End Get
			m_FirstName = Value
		End Set
	End Property
	Private m_FirstName As String
	Public Property LastName() As String
			Return m_LastName
		End Get
			m_LastName = Value
		End Set
	End Property
	Private m_LastName As String
	Public Verified As Boolean
	Private Property Password() As String
			Return m_Password
		End Get
			m_Password = Value
		End Set
	End Property
	Private m_Password As String

	Public Sub New()
		Orders = New List(Of Order)()
	End Sub
End Class



Consider using our Q&A forum for asking questions.

6 Responses to “Email template engine”

  1. UroŇ° Says:

    This is a very usefull thing and easy to implement thanks to your tutorial! Great job.

  2. Martin Staael Says:

    I tried using DataFrom with a Dictionary but it doesn’t work. Would be nice to have

  3. Limilabs support Says:


    You can use Template.AddKey method:

    string s = Template
        .Create("Show some number [key].")
        .AddKey("key", "42")

    I’ve already implemented this feature, and it’ll be added in the next release.

  4. Send email to multiple recipients Says:

    […] Also take a look at Mail.dll’s templating engine […]

  5. Martin Says:

    How would an html template look if I needed a logo image (jpg or png) in the signature part of the email ?

  6. Limilabs support Says:


    Your HTML must include img tag with src attribute set to content-id of the attached image:

    <img src="" />

    Also you need to add this image to Visuals collection:

    MailBuilder builder = new MailBuilder();
    builder.Html = @"<img src="""" />";
    MimeData logo = builder.AddVisual(@"c:\image.jpg");
    logo.ContentId = "";
    IMail email = builder.Create();

    Using fluent interface:

    IMail email = Fluent.Mail
        .Html(@"<img src="""" />")

    You can find more details here: