I am creating an email engine in mvc3 and I am trying to use razor views as email templates. I heard this is possible but I have not yet found any information about it.
-
https://scottsauber.com/2018/07/07/walkthrough-creating-an-html-email-template-with-razor-and-razor-class-libraries-and-rendering-it-from-a-net-standard-class-library/ – Ragesh P Raju Feb 25 '19 at 13:00
5 Answers
You can use http://razorengine.codeplex.com/ to achieve this. It allows you to use razor outside of mvc.
string Email = "Hello @Model.Name! Welcome to Razor!";
string EmailBody = Razor.Parse(Email, new { Name = "World" });
It's simple to implement and it's available on http://nuget.codeplex.com/ for easy integration into your projects.

- 7,643
- 12
- 52
- 65

- 26,529
- 10
- 84
- 95
-
I will definetely check this one out @BuildStarted. One question though : So If I understand correctly this is not build into razor by default? – sTodorov Dec 07 '10 at 08:52
-
looks great. One more question though: Can you still use *.cshtml "pages" as the templates? – sTodorov Dec 07 '10 at 09:24
-
No, it's not built into razor by default. Good question. While I'm sure it can process cshtml pages via razor it doesn't support it out of the box in that you'd have to add an additional template to support features such as `@Html` functionality. It might be worth requesting at the codeplex page for that support. Though MVC has it's own razor parser that's actually separate from razor itself so I'm not sure if it can be done that easily. – Buildstarted Dec 07 '10 at 15:00
-
Thanks for that @BuildStarted - very useful utility/library to know about for those times when email processing is done in a Windows Service, for instance. – HenningK Nov 18 '11 at 09:43
-
Not sure if this works with strongly typed models on the view? The @model keyword doesn't seem to be supported. – Zaid Masud Mar 22 '12 at 16:59
-
`@model` is an mvc only thing. It's been updated to support `@model` since I added that feature a while back. The latest source is at https://github.com/Antaris/RazorEngine – Buildstarted Mar 22 '12 at 17:08
-
1
You CAN use a template file to serve as a razor email body template. You can use whichever extension you choose because you can load a file as text in .Net. Let's use the following example for the template:
Hello @Model.Name,
Welcome to @Model.SiteName!
Regards,
Site Admins
Save that file as something like "WelcomeMessage.cshtml", "WelcomeMessage.template", etc. Select the file in Solution Explorer and in the Properties window, select "Copy to Output Directory" and choose "Copy Always". The only down point is that this template has to accompany the application and doesn't compile as a class.
Now we want to parse it as a string to assign to a mail message body. Razor will take the template and a model class, parse them, and then return a string with the necessary values. In your application you will need to add the RazorEngine package which can be found with NuGet. Here's a short code example to illustrate the usage:
using System.IO;
using RazorEngine;
// ...
MyModel model = new MyModel { Name = "User", SiteName = "Example.com" };
string template = File.OpenText("WelcomeMessage.template").ReadToEnd();
string message = Razor.Parse(template, model);
It's similar to the other answers but shows a quick way to load the template from a text file.

- 15,022
- 3
- 41
- 74

- 12,882
- 13
- 71
- 101
You should perhaps consider MvcMailer. RazorEngine is (very) good if you aren't already using MVC (I've used it successfully in a webforms context), but if you have MVC you may as well take advantage of it.

- 469
- 6
- 9
-
3Note that MvcMailer requires a `HttpContext`, which is why RazorEngine is a good choice for non-MVC users, or if you are planning to send emails from a background task. – Dunc May 07 '15 at 14:30
You can also use Essential Mail: Razor package from NuGet. It is build over RazorEngine and provides simple interface for email rendering.
Email message template looks something like
@inherits Essential.Templating.Razor.Email.EmailTemplate
@using System.Net;
@{
From = new MailAddress("example@email.com");
Subject = "Email Subject";
}
@section Html
{
<html>
<head>
<title>Example</title>
</head>
<body>
<h1>HTML part of the email</h1>
</body>
</html>
}
@section Text
{
Text part of the email.
}
Read more on GitHub: https://github.com/smolyakoff/essential-templating/wiki/Email-Template-with-Razor

- 735
- 1
- 7
- 14
Mailzor
Linked to what @thiagoleite mentioned, I took Kazi Manzur Rashid's idea (with permission) and extended in to be more friendly for how I wanted to use it.
So check out the github project 'mailzor'
It's also up on Nuget.org/packages/mailzor

- 4,156
- 3
- 43
- 63
-
In case it's necessary the disclosure note is it's something I'm maintaining/working on. – Nick Josevski Dec 20 '12 at 04:35