Generate HTML Email Text using Razor in .Net Console Application

10 Apr 2014, 10:45 AM

In one of my projects there was a requirement to send the emails to the users from console application for their subscription. There were many solutions that first comes in my mind to create email body dynamically for each user e.g. contact string for each email in code, create template file and put pre define tokens in it that will be replaced later from code. But I did not like all these solution and tried to find some better to do it.

Finally I found that we can user Razor Engine out the web application and it can be easily used in the windows and console application too. It is a very intuitive to generate final output text from template.

Create a new console application in your application and Name it “GenerateEmailUsingRazor” or whatever you want.

Once the project has been created add the RazorEngine package using Nuget. Right Click on the solution and click on “Manage Nuget Packages…” . In the popup window search for “RazorEngine” and install it in your application as you can see in image below.

Create UserDetail model class that will contain all the data to generate email text using template.

 namespace GenerateEmailUsingRazor.Model

    public class UserDetail

        public int Id { get; set; }

        public string Name { get; set; }

        public string Address { get; set; }

        public List<string> PurchasedItems { get; set; }



Now you need to add the Razor file to create your email template. Create new folder “EmailTemplates” in your application and add new html file “InviteEmailTemplate.html” in to the folder. It is not a simple HTML file that you need to create so rename the file and change its extension to “.cshtml”. so the name of the file will be “InviteEmailTemplate.cshtml”.

Right click on the InviteEmailTemplate.cshtml file and click on the Properties and in the property window set “Copy to Output Directory = Copy always”. As you can see in image below.

RazorEngine API provides the TemplateService class to parse the template file. Pass the template file test and model to the Parse method that returns the generated output text. See the code below to

    class Program
        static readonly string TemplateFolderPath = 
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EmailTemplates"); static void Main(string[] args) { var model = GetUserDetail(); var emailTemplatePath = Path.Combine(TemplateFolderPath, "InviteEmailTemplate.cshtml"); var templateService = new TemplateService(); var emailHtmlBody = templateService.Parse(File.ReadAllText(emailTemplatePath), model, null, null); Console.WriteLine(emailHtmlBody); Console.ReadLine(); } private static UserDetail GetUserDetail() { var model = new UserDetail() { Id = 1, Name = "Test User", Address = "Dummy Address" }; for (int i = 1; i <= 10; i++) { model.PurchasedItems.Add("Item No " + i); } return model; } }

Download Source Code

Click here to get source code

About Blog
Explains how to generate text from template using Razor engine in C#

Date 10 Apr 2014, 06:11 AM
Views 7,402