Home Contact

Igor Milovanović

.NET, cats and more...

A Simple SMTP Server Mock for .NET

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:  public void 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.");
   8:      smtpServerMock.Stop ();
   9:   
  10:      Assert.AreEqual (1, smtpServerMock.Sessions.Count);
  11:      SmtpSession session = (SmtpSession) smtpServerMock.Sessions[0];
  12:      Assert.IsTrue (session.SessionProtocol.IndexOf("somebody@foo.com") > 0 );
  13:      Assert.IsTrue (session.SessionProtocol.IndexOf("everybody@bar.com") > 0 );
  14:      Assert.IsTrue (session.SessionProtocol.IndexOf("This is the subject") > 0 );
  15:      Assert.IsTrue (session.SessionProtocol.IndexOf("This is the body.") > 0 );
  16:   
  17:  }

Example: Unit-Test with SMTP-Server Mock

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).

[1] Dumbster - Fake SMTP Server for Java
[2] SmtpMock.cs

 


Monday, September 27, 2004 7:12 PM

Feedback

# re: A Simple SMTP Server Mock for .NET

Igor,

That's really cool. Thanks for sharing.

Dave
9/27/2004 7:34 PM | David Totzke

# re: A Simple SMTP Server Mock for .NET

Hi,

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".

Can you help me out here?

Best regards,
Dadi 10/24/2005 11:16 AM | Dadi Ingolfsson

# re: A Simple SMTP Server Mock for .NET

How to handle events such as on receipt of SMTP message (then do something), when sending application is external (not part of, as in your example) to mock SMTP server? 3/10/2007 11:05 PM | David Luu

# re: A Simple SMTP Server Mock for .NET

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 7/29/2008 10:56 PM | Salome Alejandro Mancilla-Chavez

# re: A Simple SMTP Server Mock for .NET

If you are running an integration test and not necessarily need an embedded SMTP server, there's an online fake SMTP server here:

http://www.ximailstop.com 4/19/2009 11:58 PM | Shi Yan

# re: A Simple SMTP Server Mock for .NET

I think the locking on "this" might be causing a big delay on methods invoked against the SimpleSmptpServer.
8/15/2009 12:35 AM | The Stop takes forever

# re: A Simple SMTP Server Mock for .NET

Its a pretty cool & simple implementation. Going to help in testing email sending functionality. Thanks. 12/9/2009 9:51 PM | Web Developer

# re: A Simple SMTP Server Mock for .NET

@Salome Alejandro Mancilla-Chavez

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:

while (true)
{
using (Socket clientSocket = _smtpListener.AcceptSocket())
{
SmtpSession session = new SmtpSession(clientSocket);
_sessions.Add(session);
Thread sessionThread = new Thread(session.Process);
sessionThread.Start();
while (sessionThread.IsAlive)
{
Thread.Sleep(200);
}
}
/*sessionThread.Join (); // remove comment for more sessions at the same time*/
}
4/6/2011 9:06 PM | vman

# re: A Simple SMTP Server Mock for .NET

Ok good
ralph lauren polo outlet 7/9/2011 10:37 AM | ralph lauren big pony polo

# re: A Simple SMTP Server Mock for .NET

We are using Mailtrap - http://mailtrap.io web service.

It give you remote smtp server account and direct access to all mails in it.
11/30/2011 9:23 AM | Bogdan Gusiev

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: