A SMTP server mock is basically a fake SMTP server which can be used for unit testing of applications which send email messages. It acts as a real smtp server, except that the incoming messages are locally stored and not actually delivered . This can be quite usefull if you are using real data for testing. ;-)

1: [Test]

2: publicvoid MailTest ()

3: {

4: SmtpMock smtpServerMock = new SmtpMock ();

5: smtpServerMock.Start();

6: System.Web.Mail.SmtpMail.SmtpServer = "localhost";

7: System.Web.Mail.SmtpMail.Send("somebody@foo.com", "everybody@bar.com", "This is the subject", "This is the body.");

As I couldn't find a .NET implementation (take a look at dumbster for java version), I decided to write one myself. The implementation is very simple as it only opens a listener at port 25 and responds to pretty much anything with 250 OK. ;-) . The recieved emails are stored in SessionProtocol (see example).

This is cool and I´d like to use it, but I get some error I´m unable to resolve.

If I run your example I get: "System.IndexOutOfRangeException : Index was outside the bounds of the array"

If I debug through it I get an exception on SmtpMail.Send that says:
"An unhandled exception of type "System.Net.Sockets.SocketException" occurred in Unknown Module. Additional information: Only one usage of each socket address (protocole/network address/port) is normally permitted".

I've used your example, I made the adjusment to make it work with .Net Framwork 3.5. But I still having an Issue, The STMPServer never stop, so my unit test never ends. can you tell me where might be the problem

It looks like the stop is called but the socket is still open so it just hangs. It’s a bit cheap but for a quick-fix I essentially made the session.Process synchronous so I could clear down the socket (with a ‘using’), as below: