Lambert on Development

My thoughts on software development

Lambert on Development RSS Feed
 
 
 
 

Use a null SMTP server for testing

It’s not uncommon for companies to have SMTP servers locked down so that only a limited set of machines are able to send email. They might do this by requiring authentication or by blocking port 25 for all but “known” server hardware. This makes a great deal of sense in the face of zombies and spam and all manner of email-propagated evil, but when you’re testing an email-enabled application, it can put a real cramp on your progress.

In some cases, you can get away with running a full-blown SMTP / POP server on your desktop, or maybe you’ve got the freedom to set up a machine or a VM. This can be more hassle than it’s worth, though, in some cases. It’s a hassle to set up, it’s going to suck up a noticeable chunk of resources on your desktop, and maybe the biggest issue – you don’t really want to deliver all those emails!

DevNullSmtp console

DevNullSmtp console

Here’s an alternative that works really well.  Use a null SMTP server.  I found one called DevNullSmtp, but there are others available, too.  DevNullSmtp is a 458K Java JAR file, and it requires Java 1.4 or better to run.  In my case, I used a Java HotSpot VM, and I had no problems at all.

Setup is a snap – I created a shortcut to launch the console, and then clicked a button to start the dummy server.  I changed my application’s configuration to use “localhost” as the SMTP server, and I was up and running.

One of the unanticipated benefits of using this server for email is that you don’t have to worry about setting up dummy email addresses for all your receipients because you don’t want them to get test emails.  All the emails simply disappear into this neat little black hole on your desktop.  Of course, you can save all the emails that are sent through this server so you can go back later and check out their contents, and again – this turns out to be a pretty great tool for testing, because you can compare emails generated in a baseline run to emails in a test run to see if they’ve changed.  Not so easy when you’re using real email, right?

This definitely goes on my short-list of development tools – it’s already saved me a ton of time.

Note: I’ve already got a couple of amendments to add.  First, if you want a *really* simple mock server written in C#, you can get one here:  http://code.msdn.microsoft.com/fakesmtpservice

Now, here’s something even more interesting – I found this while unit testing.  I found that if you connect to either of these fake SMTP servers immediately after disconnecting from a previous send, you’ll block on the open TCP/IP socket.  I found that a delay of 100ms or so worked fine to allow the socket to clear so it could accept a connection again.  You can play with that value if you want.  For unit testing, I just put this delay in my [TestCleanup()] routine (the init routine would work, as well).

The code below shows the delay within a single routine – without that pause in the middle, this routine blocks and fails.


[TestMethod()]
public void BasicSMTPTest()
{
SmtpClient client = new SmtpClient("localhost");
using (MailMessage msg = new MailMessage("me@nowhere.com", "you@nowhere.com", "subj", "Test msg"))
{
client.Send(msg);
}
client = null;
// got to be a delay between these requests, or we'll block port 25
Thread.Sleep(100);

SmtpClient client1 = new SmtpClient("localhost");
using (MailMessage msg1 = new MailMessage("me@nowhere.com", "you@nowhere.com", "subj", "Test msg"))
{
client1.Send(msg1);
}
}

[Update: Oct 2, 2009] –  I just read an article on The Accidental Geek that describes two more products that do approximately the same thing:  PaperCut and Neptune.  I haven’t had a chance to try either one of these products yet, but based on Joe’s experience, it may be worth checking them out.

Reblog this post [with Zemanta]

Post to Twitter Tweet This Post

blog comments powered by Disqus

Similar Posts

Google Reader

Blogroll

Blog Directories

 
Programming Blogs - Blog Catalog Blog Directory
 
Computers Blogs - Blog Top Sites
 
Banner
 
Lambert on Development - Blogged

Recent Posts

Popular Posts

Support this Site

Featured Articles

Lijit Search

Lijit Search