Sunday, August 24, 2014
Did I ever tell you the story about how I didn't know my laptop had a microphone for 3 years? Once upon a time, I was forced to buy a laptop from a school for way too much money and the laptop didn't have a camera. So, I assumed the laptop didn't have a mic. About three years had passed and my resentment of not having a built-in camera to use in countless unproductive ways, had faded. One day I was getting my wired mic to use the video/voice chat program du jour and noticed the mic volume gauge (I think that's what it's called) moving and thought that was strange. Once that happened, I talked to myself and it moved again. At this point I started feeling my laptop and sure enough I found out where the mic was (and that I wasn't physically attracted to my laptop).
The point of that story was to demonstrate how I found Postal, a new email sending library for ASP.NET MVC. You see, I used ActionMailer.Net to generate my emails and it's pretty good, but then I started another project at work and it also required email notifications, so I decided to see if there was something better. Keep in mind, ActionMailer.Net.Net isn't bad, but I like to try and find better tools like a good boy that solves problems (I'm not calling myself a programmer because of a blog post I recently read). So, I did a quick Google-assisted scan of the Internet, and I came across Postal.
What's the difference between ActionMailer.Net and Postal? Well, I've only used it for basic automated notification emails, but I would say the main difference is flexibility. ActionMailer.Net forces you to create a MailController, where Postal allows you to create an email object and send it right away. Of course, with Postal you still have to follow some conventions to get it to send properly and your most likely going to create a service to keep all your email logic out of your controller. As I write this, I'm definitely starting to see that using one or the other depends on your style, and where you want to put the code into. Either option will allow you to get started pretty quickly, but Postal seems a little friendlier at first with the most basic emailing option. And, now that I look back at ActionMailer.Net, another reason to use Postal is because ActionMailer.Net is no longer maintained, which is explained in the link to ActionMailer.Net.
To see what I mean about flexibility, here's a quick initial code comparison of examples I took from their sites:
public class MailController : MailerBase
{
public EmailResult VerificationEmail(User model)
{
To.Add(model.EmailAddress);
From = "no-reply@mycoolsite.com";
Subject = "Welcome to My Cool Site!";
return Email("VerificationEmail", model);
}
}
@using ActionMailer.Net.Net
@model User
@{
Layout = null;
}
Welcome to My Cool Site, @Model.FirstName
We need you to verify your email. Click this nifty link to get verified!
@Url.AbsoluteAction("Verify", "Account", new { code = @Model.EmailActivationToken.ToString() })
Thanks!
new MailController().VerificationEmail(newUser).Deliver();
To: @ViewBag.To
From: lolcats@website.com
Subject: Important Message
Hello,
You wanted important web links right?
Check out this: @ViewBag.FunnyLink
<3
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();
}
}
I left out some of the details in order to focus on the immediate code you need to write to get things working. The two libraries generally find the views for the emails in a similar way, so I didn't bother showing it. I'll admit this is shallow comparison, but I kinda like the flexibility you get right away with Postal. Either way, both are great libraries and will get you away from StringBuilder'ing yourself to death.
Until next time, don't forget to relax.