views:

1501

answers:

10

I need to configure a SMTP server for testing my website which sends emails (for registration confirmation etc).

I dont actually want the email to be sent, I just want to make sure that my code is correct. So I want to be able to check that the email is placed in a queue folder for example.

Can anybody recommend a SMTP server which is easy to configure?

+4  A: 

I think the blog post A Simple SMTP Server Mock for .NET gives you what you need: a SMTP server mock

A SMTP server mock is basically a fake SMTP server which can be used for unit testing of applications which send email messages.

Also, a google search for smtp mock server will provide you with a selection of SMTP servers for testing purposes. Like:

f3lix
Dumbster lets your unit test start the SMTP service, test some sending code, and then make assertions about how many e-mails were sent, what their contents were, and so on.
Don Kirkby
+2  A: 

An alternative way to do this is to create a wrapper around the SmtpClient that implements the same interface. Then inject and use the wrapper in your class. When doing unit testing you can then substitute a mock wrapper that has expectations for the method calls and responses.

EDIT: The wrapper is needed (for RhinoMocks, at least) because SmtpClient doesn't derive from an interface and doesn't have virtual methods. If you use a mocking framework that can mock a class without virtual methods directly, you can skip the wrapper and inject the SmtpClient mock directly.

public class SmtpClientWrapper
{
    private SmtpClient Client { get; set; }

    public SmtpClientWrapper( SmtpClient client )
    {
         this.Client = client;
    }

    public void Send( MailMessage msg )
    {
         this.Client.Send( msg );
    }

    ...
}


public class MyClass
{
    private SmtpClientWrapper Client { get; set; }

    public MyClass( SmtpClientWrapper client )
    {
         this.Client = client;
    }

    public void DoSomethingAndNotify()
    {
         ...
         this.Client.Send( msg );
    }
}

Tested (with RhinoMocks) as:

public void DoSomethingAndNotifySendsAMessageTest()
{
     SmtpClientWrapper client = MockRepository.GenerateMock<SmtpClientWrapper>();
     client.Expect( c => c.Send( new MailMessage() ) ).IgnoreArguments();

     MyClass class = new MyClass( client );

     class.DoSomethingAndNotify();

     client.VerifyAllExpectations();
}
tvanfosson
+12  A: 

There's also Papercut which is an SMTP server which will receive messages but not deliver them anywhere (allowing you to make sure they are being sent correctly). The received messages are visible in a small GUI and are also written to a directory.

Sean Carpenter
Papercut has moved from the URL above to Codeplex: http://papercut.codeplex.com/
Drarok
I updated the link, @Drarok.
Don Kirkby
+2  A: 

The DevNull SMTP server logs all the gory details about communication between the client and the SMTP server. Looks like it would be useful if you were trying to diagnose why your sending code wasn't working.

It's written in Java and deploys as an executable jar. Source code doesn't seem to be available.

Don Kirkby
+2  A: 

The smtp4dev project is another dummy SMTP server. I like it because it has a nice, simple UI that logs the messages and lets you view the contents of recent messages. Written in C# with an MSI installer. Source code is available.

Don Kirkby
A: 

there's also my very own http://ssfd.codeplex.com/ which is an open source SMTP emulator. Receives e-mail and drops them in a folder which can be accessed by a task icon

Anthony Johnston
+1  A: 

If you are on Mac OS X you can use MockSMTP.app

Sébastien Gruhier
+5  A: 

In .NET, SmtpClient can be configured to send email by placing it in a pickup directory.

The default constructor of SmtpClient takes its settings from app.config, so for a test environment we can configure it as follows.

<configuration>
    <system.net>
        <mailSettings>
            <smtp deliveryMethod="specifiedPickupDirectory">
                <specifiedPickupDirectory> … </specifiedPickupDirectory>
            </smtp>
        </mailSettings>
    </system.net>
</configuration>

MSDN reference - app.config mailSettings element http://msdn.microsoft.com/en-us/library/w355a94k.aspx

Lachlan Roche
beaten!! left the page open and answered without refreshing it.
argatxa
A: 

For .NET guys out there. Keeping it simple.

We were looking into this and then one of the developers remembered about a the config setting that allows you to override how the emails are sent.

This will create a file per email and leave it alone.

<system.net>
    <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory">
        <specifiedPickupDirectory pickupDirectoryLocation="\\SharedFolder\MailDrop\" />
      </smtp>      
    </mailSettings>
  </system.net>
argatxa
+1  A: 

You can also use netDumbster.

http://netdumbster.codeplex.com/

Carlos Mendible