When you need to generate and send templated emails, consider mailzor

Mailzor is a basic utility library to help generate and send emails using the Razor view engine to populate email templates, designed to be quickly pluggable into your .NET app.

In our applications we send out HTML formatted emails, and seed them with a variety of data. I thought it would be easy to write them as razor files (cshtml) and then use the razor engine to generate them and send.

It’s up on NuGet and with the release of v1.0.0.11, it’s more stable.

For the most up to date info follow along with the usage sections of the readme.md file on the github repository.

How it works

I thought I would share some background about the development of it, and hiccups along the way. The original set of code came from Kazi Manzur Rashid, which solved the problem of making use of System.Web.RazorTemplateEngine, which I extended (with permission) to be usable as an injectable dependency and via NuGet.

The core elements are, the creation and management of the SMTP client, the building up of the MailMessage. Then all the compilation related work to get the RazorTemplateEngine up and running.

The RazorTemplateEngine logic boils down to taking the razor file stored on disk and using CSharpCodeProvider.CompileAssemblyFromDom. So if you’re curious about this code in particular dig into EmailTemplateEngine.cs in the project files.

Prior to version .10 where I went down the path of using ilmerge to solve conflicts of version mismatch with System.Web.Razor.

It seems easy seeing how I took an existing chunk of operational code and extended, and it only seems easy when it is working, but when it doesn’t work and you’ve got strange compilation errors, debugging this mechanism is not the greatest. I found myself hunting for temporary files and trying to have other compiler flags to output more information.

In the early versions it was heavily the case of “works on my machine”, but now its fine and seems to be feature complete…

Advertisements

2 thoughts on “When you need to generate and send templated emails, consider mailzor

  1. Hi,
    using Mailzor, how to send an email to multiple email recipient ?

    I loaded a list to “To” field

    subsystem.SendMail(
    “NewTaskEmailCommand”,
    new NewTaskEmailCommand
    {
    To = addressList,
    From = “source@somedomain.com”
    });

    where addressList is :

    List addressList = new List();
    addressList.Add(“email1@xxxxx.it”);
    addressList.Add(“email2@gmail.it”);
    addressList.Add(“email3@xxxxx.it”);

    On EmailTemplateEngine.Execute method

    foreach (var pair in templates.Where(x => !x.Key.EndsWith(“.dll”)))
    {
    pair.Value.SetModel(WrapModel(model));
    pair.Value.Execute();
    }

    I receive
    The best overloaded method match for ‘System.Collections.Generic.ICollection.Add(string)’ has some invalid arguments

    Any suggestion please ?

    Thank’s

    Claudio

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s