Ever had to resort to custom code to control emails sent by an application during integration and/or system testing? If you answered yes then you should definitely continue reading.
NFakeMail makes it easier for developers to do integration/system testing on software that sends emails by providing a fake SMTP server. You’ll no longer have to manually validate the email sending process. It’s developed in C# and IronPython and targets the .
NET 4.0 framework.
With NFakeMail you can easily automate the testing of components that rely on sending mails while doing its job. Let’s take a look at some sample code, we start with a simple class containing a method that sends emails.
class Notifier
{
public void Notify()
{
using (var smtpClient = new SmtpClient("localhost", 10025))
{
smtpClient.Send("
[email protected]", "
[email protected]", "S1", ".");
smtpClient.Send("
[email protected]", "
[email protected]", "S2", "..");
}
}
}
Then to automate the tests for this method we only need to the following:
[Test]
public void Notify_T001()
{
using (var server = new FakeSmtpServer(10025))
{
new Notifier().Notify();
// Verifies two messages are received in the next five seconds
var messages = server.WaitForMessages(count: 2, timeout: 5000);
// Verifies the message sender
Debug.Assert(messages.All(m => m.From.Address == "
[email protected]"));
}
}
The created FakeSmtpServer instance will act as a simple SMTP server and intercept the messages sent by the Notifier class. It’s even possible to verify some fields of each intercepted message and by default all intercepted messages are saved to the file system in MIME format.