Razor treats as email templates

I am creating an email engine in mvc3 and I am trying to use razor views as email templates. I heard that this is possible, but I have not yet found any information about this.

+52
templates asp.net-mvc-3 razor
Dec 6 2018-10-06
source share
5 answers

You can use http://razorengine.codeplex.com/ to achieve this. This allows you to use a razor outside of mvc.

string Email = "Hello @Model.Name! Welcome to Razor!"; string EmailBody = Razor.Parse(Email, new { Name = "World" }); 

It is easy to implement and is available at http://nuget.codeplex.com/ for easy integration into your projects.

+61
Dec 06 '10 at 21:45
source share

You can use the template file as a razor email body template. You can use any extension you choose because you can upload the file as text to .Net. For example, use the template:

 Hello @Model.Name, Welcome to @Model.SiteName! Regards, Site Admins 

Save this file as "WelcomeMessage.cshtml", "WelcomeMessage.template", etc. Select the file in Solution Explorer and in the Properties window, select Copy to Output Directory, and select Copy Always. The only drawback is that this template must accompany the application and does not compile as a class.

Now we want to parse it as a string to assign to the mail body. Razor will take the template and class of the model, analyze them and return a string with the necessary values. In your application, you will need to add the RazorEngine package, which can be found on NuGet. Here is a short code example illustrating 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 is similar to other answers, but shows a quick way to load a template from a text file.

+27
Mar 09 '11 at 15:31
source share

Perhaps you should consider MvcMailer . RazorEngine is (very) good if you are not using MVC yet (I have used it successfully in the context of webforms), but if you have MVC, you can also use it.

(via Hanselmen NuGet Week 2 Package )

+13
May 13, '11 at 6:26 a.m.
source share

You can also use Essential Mail: Razor from NuGet. It is built on RazorEngine and provides a simple interface for email rendering.

The email template looks something like this:

 @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. } 

More on GitHub: https://github.com/smolyakoff/essential-templating/wiki/Email-Template-with-Razor

+4
Feb 18 '14 at 7:13
source share

Mailzor

Related to what @thiagoleite mentioned, I accepted the idea of ​​Kazi Manzur Rashid (with permission) and expanded it to be more friendly with the way I wanted to use it.

So check out the gizub mailzor project

It is also located at Nuget.org/packages/mailzor

0
Dec 20
source share



All Articles