Using SMTP4Dev for integration tests

Dec 2, 2010 at 8:38 AM



Can I start smtp4 from with a unit test and get the recieved mails.

I would love som example code for that. This would allow you to for example when using watin form ui-testing assert that a mail was sent etc.



Dec 6, 2010 at 8:24 PM
Hi thanks for your question. Yes, it's easy to use smtp4dev's smtp engine to perform automated tests. Here's an example:
List<Message> messages = new List<Message>();

DefaultServer server = new DefaultServer(Ports.AssignAutomatically);
server.MessageReceived += (s, ea) => messages.Add(ea.Message);

//do something to send mail here
//you can get the port number to use from server.PortNumber
Assert.Equals(1, messages.Count);
You'll need to add a reference to the Rnwood.SmtpServer assembly and add a using for the Rnwood.SmtpServer namespace to get this to compile.
You can replace Ports.AssignAutomatically with a literal port number if you always want it to run on the same port number or use one of the other overloads (and provide a certificate) if you want to use SSL.
If you want to examine the message in more detail than the properties of the Message class can provide then you'll need to feed the "Data" from the message into sharpmimetools (included with smtp4dev). You can see how smtp4dev does this here: