When writing web applications, one often has to send email as a part of the notification or registration process.
Sending email through an SMTP server directly from your main web application thread can block for an unreasonable amount of time while serving a response page that is waiting for the email to be delivered.
One solution might be to simply fire up a worker thread for processing SMTP sending operations; and I’m sure many people have successfully employed that approach.
A more appealing strategy for me is to simply write the email out to a queue and allow some completely separate process handle the delivery of the email.
This has the advantage of clean separation of concerns, and improved responsiveness in handling HTTP requests because we’re not waiting on SMTP delivery to complete before returning the HTTP response.
There is a potential downside in not being able to get immediate feedback about failed delivery attempts, but that is not an insurmountable problem.
IIS of course has a very nice mail processing facility. In fact, if you’re writing an ASP.NET MVC web application that will always be running on IIS (instead of, say, in an OWIN environment and a standalone server environment), then it’s a great solution.
Also, Microsoft Exchange has the same ability to monitor a mail delivery folder and batch sending of emails that are delivered to that folder.
I believe there are other solutions for the Windows environment, and Linux-like environments have long had tools for delivering emails that are dropped into a specified folder.
However all of these solutions were either too coupled to a specific infrastructure or platform, or had too high of a total cost of ownership, for me to be fully comfortable in using them.
Enter Mail Queue Dispatcher
The code is open source, and uses the MIT license; which should be very compatible with even the most stringent licensing restrictions in a corporate environment.
Because my primary use has been in a Windows environment, the documentation and installation is Windows specific, but it would be trivial to deploy and use the same tool on a non-Windows environment.
As it is, you can install the Windows service that drives the dispatcher, and perform some simple configuration as to which folder new mail will be dropped into, and which folder failed attempts will be moved to.
Also there is some useful logging which will help pinpoint and diagnose any delivery issues.
Security and Configuration
An issue I faced was that I only want to be able to send emails from well known and authenticated email addresses.
This implementation is not an “Open Relay”, all sender email addresses have to be configured and the configuration file will hold the SMTP credentials for sending the email appropriately.
I’ve been using this library successfully in a Production environment for a few years now and it’s worked flawlessly.