Razor provides a nice, clean template syntax. It works well for MVC apps and as I'll show you in this blog post, it works well for email message templating too. The basis of this blog post is a question on StackOverflow that I answered. Building email message content via string concats can be a huge pain to implement (not to mention pretty boring).
My method for email templating with C# is sweet and simple. It works by reading the text from a template file at runtime, retrieving a model object, and then passing both to the Razor parser to render the needed email body with the model property values populated. Since the templates are read at runtime, small cosmetic tweaks to your email templates are easy.
Here's a breakdown of what you need
- Reference RazorEngine (Nuget package available)
- Create a template file
- Render email body with RazorEngine's parser
- profit!
RazorEngine
In order to use the RazorEngine parser, you have to reference the RazorEngine DLL.The easiest way and recommended way to accomplish this is to install the Nuget package. Search for "RazorEngine" with the package manager, and add it to include it in your projectCreate Your Email Body Template
Add a new file to your Visual Studio project. Something named "EmailBodyTemplate.cshtml" should do the trick. Note: if the project is anything but an MVC project and you're adding the template with the "Add New Item" dialog, just use the generic blank "Code File" item. Adding the ".cshtml" extension isn't completely important unless you want Visual Studio to edit the file with the Razor editor so intellisense and code completion is available.Now that we have a fresh new blank Razor template, let's make use of it. The example template is for a contest winner notification.
Hello @Model.WinnersName, You were chosen as the @Model.ContestName winner! Please contact us immediately
at @Model.RedemptionPhone to claim your winnings. Sincerely, @Model.CompanyName
How nice is that? Beautiful simplicity. No concating together string chunks with properties!
You will most likely want to change the properties for the template in the project so when you build it, the template file(s) are copied along with the assembly. Select the file in "Solution Explorer", go the file's properties and change the "Copy to Output" property from "Never" to "Copy Always" so it gets copied during the build process.
Render the Email Body
Since we have our template, we can put it to work in an app. A ContestWinnerNotifcationModel class needs to be defined that contains properties with names that match the model properties referenced in the template.
public class ContestWinnerNotificationModel
{
public string WinnersName { get; set; }
public string ContestName { get; set; }
public string RedemptionPhone { get; set; }
public string CompanyName { get; set; }
}
Now for the meat'n'potatoes. Let's instantiate a model, read the template file, and render the final product.
using RazorEngine;
using System.IO;
// ...
var model = new ContestWinnerNotificationModel()
{
WinnersName = "Taco Bob",
ContestName = "Boat Load of Tacos Contest",
RedemptionPhone = "555-TACO",
CompanyName = "Taco's Unlimited, Inc."
}
string template = null;
using(StreamReader reader = File.OpenText("EmailBodyTemplate.cshtml"))
{
template = reader.ReadToEnd();
}
string renderedEmailBody = Razor.Parse(template, model);
// Prep, process, and send the winner notification ...
Profit
That's it! Really! From there you can change the template around when needed but make sure that your production code does proper file checking to validate that the template file can be read, that it exists, etc.
Another possibility for using Razor for email templating is html email content. If you use the Razor editor to edit your templates, you also have the luxury of having intellisense/code completion for html tags.