Unit-testing System.Net.Mail.SmtpClient

On a recent project I wanted to write unit-tests for a class using a System.Net.Mail.SmtpClient instance. In certain situations, the class should send an email with an attachment. Before I started, I thought I’d simply mock the ISmtpClient interface for this purpose … however, such an interface does not exist.

After doing some research on the web, I came up with the following possible solutions:

  1. Dumping the mails on disk, by configuring System.Net.Mail.SmtpClient to use a deliveryMethod of type SpecifiedPickupDirectory (example)
  2. Running a local smtp server to catch my test mails (and then somehow inspect those emails to see if they meet my requirements)
  3. Writing a mockable wrapper (e.g. implementing “ISmtpClientWrapper”) and pass this wrapper to my consuming class
  4. Run a simple SMTP server within my unit tests

Of these options, the last one appeared the least intrusive to me. I first used nDumpster (a .NET port of the Java project Dumpster), however it would cause my unit tests to hang. Before figuring out why, I switched to netDumpster, which has (about) the same API. netDumpster ran just fine and enabled me to run my unit tests fast enough.