Easy Email Templates in .NET with FluentEmail

Update: The “Replace” method of templating in Fluent Email has itself been replaced by a Razor Engine Template. See Luke’s Post here about it.

I always seem to forget how to send emails in .NET which to me seems like such a simple task. Lately I’ve been working on updating an older application of mine that’s soul purpose was to read a database and send emails depending on certain events. I had a look at the current code and saw something like this:

sb.Append("A Transaction has been made...<br />");
sb.Append("<table><tr><th>Reservation Number</th><th>Customer Name</th><th>Email</th><th>Sale Date</th><th>Amount</th></tr>");
sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td></tr>",
    transaction.Reservation, transaction.FullName, transaction.Email, transaction.SaleDate, transaction.Amount.ToString("C2"));
sb.AppendFormat("<tr><th>Invoice</th><th>Confirmation</th></tr><tr><td>{0}</td><td>{1}</td></tr></table>", transaction.Invoice, transaction.Confirmation);

Terrible! So I figured while I was updating it I’d work out a better way build up the HTML of the emails. I started with the FluentEmail Project (Luke’s Post about it). From there I added some basic templating features, such as loading a HTML file with placeholders for content and the rest is history…

 

With my recent updated to FluentEmail I was about to turn something that looked like the above code to this: (as always C# is the code of choice)

//Send Email!
var email = Email
            .From(_fromEmail)
            .To(toAddresses)
            .Subject(string.Format("Siriusware Emailer Confirmation #{0}", transaction.Confirmation))
            .UsingTemplate(@"C:\Emailer\TransactionTemplate.htm")
            .Replace("<%Reservation%>", transaction.Reservation)
            .Replace("<%FullName%>", transaction.FullName)
            .Replace("<%SaleDate%>", transaction.SaleDate.ToShortDateString())
            .Replace("<%Amount%>", transaction.Amount.ToString("C2"))
            .Replace("<%Invoice%>", transaction.Invoice)
            .Replace("<%Confirmation%>", transaction.Confirmation)
            .UsingClient(new SmtpClient(_mailhost));
email.Send();

 

The HTML inside TransactionTemplate.htm has the below code. This is a basic example but demonstrates the usage of the templates.

<html>
<head>
    <title></title>
</head>
<body>
    A Transaction has been made...<br />
    <table>
        <tr>
            <th>Reservation Number</th>
            <th>Customer Name</th>
            <th>Email</th>
            <th>Sale Date</th>
            <th>Amount</th>
        </tr>
        <tr>
            <td><%Reservation%></td>
            <td><%FullName%></td>
            <td><%Email%></td>
            <td><%SaleDate%></td>
            <td><%Amount%></td>
        </tr>
        <tr>
            <th>Invoice</th>
            <th>Confirmation</th>
        </tr>
        <tr>
            <td><%Invoice%></td>
            <td><%Confirmation%></td>
        </tr>
    </table>
</body>
</html>

Pretty damn easy if you ask me.

This also give me the flexibility to edit the HTML file in a proper editor instead of concatenating all those strings together. The main idea was inspired by the MailDefinition class built into .NET but I didnt like the face that: 1 it was part of the System.Web.UI.WebControls namespace when its not a Web Control and wouldnt be used as one. And 2, It attempted to use another Control as a worker for it. So I just extended MailMessage to add the functionality.

All you need to do is download FluentEmail from Github

5 comments on “Easy Email Templates in .NET with FluentEmail

  1. looks good dk. One request that might be cool – being able to replace tags with the name/email of the person you are emailing to.

    For example if I have 10 people on a cc list but want to address the email personally.

  2. Anthony says:

    Nice work!

    Whats the HTML code that you used for the template?

    Also, relative path would be a lot better then physical path because it would then allow people to deploy the templates on their websites and create standardised responses, for example, a contact form thankyou response.

  3. winladen says:

    Hey, this is a simple but great tool, thanks to FluentEmail!

  4. great blog thank you

  5. Cecildt says:

    Nice Work!

    You can extend it even further by using Xsl templates and xml input to the template.

    Example:

    Email.UsingTemplate(‘template.xsl’)
    .InputData(xml)

    Have a look at the Web Application Toolkit for Template Driven Email.

    http://code.msdn.microsoft.com/WebAppToolkitEmail

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>