Create rich Html emails using Razor Views.
Use the Razor Engine that powers MVC views. Can also be used in non MVC projects simply by adding a reference to System.Web.Razor
Dependences:
- PreMailer.Net (and CsQuery)
- System.Web.Razor
Supported:
- @functions { }
- @section <Html|Text> { }
Not Supported:
- Layout
- @helpers
Simple Email Template.
@{
From = new System.Net.Mail.MailAddress(Model.From);
To.Add(Model.To);
Subject = "Welcome to mysite.com";
}
@functions { // we support functions :)
static int _counter = 1;
int GetId() { return _counter++; }
}
@section Text { // This will be the text/plain alternative
Dear @Model.Name,
An account has been created for you.
Your account is FREE and allows you to perform bla bla features.
To login and complete your profile, please go to:
@Model.LogOnUrl
Remember, we'll never ask for your password.
Counter: @GetId()
}
@section Html {
<body>
Dear @Model.Name,<br>
<p>An account has been created for you.<br />
Your account is FREE and allows you to perform bla bla features.<br />
To login and complete your profile, please go to:<br />
<br />
<a href="@Model.LogOnUrl">@Model.LogOnUrl</a>
<br />
<img src="@EmbedResource("image.jpg")" /><br />
<b>Remember, we'll never ask for your password.</b>
</p>
</body>
}
Usage:
var service = new Postman.PostmanService();
var model = new { From = "[email protected]", To = "[email protected]", Name = "Goober Shoes", LogOnUrl = "http://www.website.com/logon/" };
var msg = service.Render("WelcomeMail.cshtml", model);
using (var smtp = new System.Net.Mail.SmtpClient())
{
smtp.Send(msg);
}