Create email using ASP.NET MVC views
Install Postal into your ASP.NET web application using Nuget.
PM> Install-Package Postal.Mvc5
Use Postal in your controller action method.
using Postal;
public class HomeController : Controller
{
public ActionResult Index()
{
dynamic email = new Email("Example");
email.To = "webninja@example.com";
email.FunnyLink = DB.GetRandomLolcatLink();
email.Send();
return View();
}
}
Create an Email
, passing it the name of the view to use for the email.
Using dynamic means we can nicely assign some data to use in the view. It's also possible to create a strongly-typed email class.
Calling Send()
will send the email using the .NET Framework's
SmtpClient
.
Postal will find the email view at Views\Emails\Example.cshtml
:
To: @ViewBag.To From: lolcats@website.com Subject: Important Message Hello, You wanted important web links right? Check out this: @ViewBag.FunnyLink <3
The email view specifies the headers and body content.
The ViewBag
contains any data you assigned to the Email
object.
Postal can work with any ASP.NET MVC view engine.
Postal uses SmtpClient
, which can be configured in Web.config
.
Not everyone likes using dynamic objects. Postal lets you strongly type your email data if you need to.
Verify that your code would send emails, without actually sending them.
Use multiple views to define the HTML and plain-text content of your emails.
Attach files to your emails.
Embed images in your emails. Postal will handle the multi-part stuff for you.
Send email from command line applications and Windows services.
Set email headers, such as CC, BCC, Reply-To, etc.
Instead of directly sending email, Postal can just create a System.Net.MailMessage
object for you.
During development it can be handy to preview generated emails in the browser. Postal can handle that with a one-liner.
Postal was created by Andrew Davey. Read his blog aboutcode.net or follow him @andrewdavey
Photo Credit: ....Tim via Compfight cc
Favicon Credit: Dakirby309 cc