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.
相关问题
- Entity Framework throws exception - Network Relate
- Google Test - generate values for template class i
- Template Specialization for Private Types
- Slow loading first page - ASP.NET MVC
- zero length variadic expansion of ill-formed call
相关文章
- “Dynamic operations can only be performed in homog
- Change color of bars depending on value in Highcha
- C++ Template specialization to provide extra membe
- How to get server path of physical path ?
- Breakpoint in ASP.NET MVC Razor view will not be h
- How to define function that returns html in asp.ne
- C++: How to use unnamed template parameters in cla
- Templates, Function Pointers and C++0x
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:
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:
It's similar to the other answers but shows a quick way to load the template from a text file.
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
You can use http://razorengine.codeplex.com/ to achieve this. It allows you to use razor outside of mvc.
It's simple to implement and it's available on http://nuget.codeplex.com/ for easy integration into your projects.
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
Read more on GitHub: https://github.com/smolyakoff/essential-templating/wiki/Email-Template-with-Razor
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.
(via Hanselmen's NuGet package of the week 2)