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.
jlafay Mar 09 '11 at 15:31 2011-03-09 15:31
source share