Elevator Pitch
Does your web application use Microsoft ASP.NET MVC?
Do you find generating emails to be a big bag of string concatenation fail?!
Postal lets you create emails using regular MVC views.
See Postal in Action
There was a session at MVCConf on Postal: Watch the video
Simple Example
Here's the simplest way to use Postal:
~\Controllers\HomeController.cs
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();
}
}
~\Views\Email\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 name of the view, "Example", is passed to the Email object's constructor.
The Email object creates a ViewDataDictionary to pass to the view. The view accesses the view data via the ViewBag dynamic object. Notice the hip new dynamic Email object that makes the syntax nice in the controller code. (Don't worry you oldies, you can strongly type the email model if you must!)
Postal puts the email headers into the actual view. Crazy?! Nope, I think it makes for a better separation of concerns actually.
The view is using Razor syntax, but aspx should also work fine.
Internally, Postal uses the .NET Framework's SmtpClient. You can configure it's behavior in your web.config file.
~\Web.config
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\email"/>
<network host="localhost"/>
</smtp>
</mailSettings>
</system.net>
Ready to "Go Postal"? ;)
Postal is available via nuget. In Visual Studio, either do "Project > Add Library Package Reference" and search for "postal" , or enter the following into the Package Manager Console:
Install-Package postal
Postal is a free, open source, library so please fork it on GitHub. Pull requests are welcome.
But wait, there's more!
The example above is very basic. You might need to do more.
- Strongly type the Email object
- Mock out email sending code for testing
- Get the generated System.Net.Mail.MailMessage object
- Send email in both text and HTML formats
- Add attachments to emails