iasksillyquestions
iasksillyquestions

Reputation: 5689

Testing SMTP with .net

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?

Upvotes: 60

Views: 34153

Answers (17)

f3lix
f3lix

Reputation: 29877

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:

Upvotes: 7

Anish John
Anish John

Reputation: 410

You can use Mailnest.io as it is an affordable and yet very effective tool for email testing. It also has a free forever plan for limited usage.

Upvotes: 0

Sean Carpenter
Sean Carpenter

Reputation: 7721

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.

Upvotes: 42

theDmi
theDmi

Reputation: 18034

As noted by Sean Carpenter, Papercut is a powerful solution for local development. If you also run a staging or testing server, however, mailtrap.io may be a simpler solution overall, because you can use the same approach for your dev and staging environments.

Upvotes: 0

Lachlan Roche
Lachlan Roche

Reputation: 25956

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 pickupDirectoryLocation="path to a directory" />
            </smtp>
        </mailSettings>
    </system.net>
</configuration>

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

Upvotes: 34

Don Kirkby
Don Kirkby

Reputation: 56660

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.

Upvotes: 22

tvanfosson
tvanfosson

Reputation: 532485

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 virtual 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 klass = new MyClass( client );

     klass.DoSomethingAndNotify();

     client.VerifyAllExpectations();
}

Upvotes: 5

Brad Parks
Brad Parks

Reputation: 72001

If you've got Python installed, you can run the following one liner to run a debug smtp server in the console that'll dump messages to stdout:

sudo python -m smtpd -n -c DebuggingServer localhost:25

snagged from here: http://muffinresearch.co.uk/archives/2010/10/15/fake-smtp-server-with-python/

Upvotes: 1

Sir CodesALot
Sir CodesALot

Reputation: 956

I use Antix SMTP Server For Developers which is as easy as opening up an application. It stores the messages in a folder and you can view them with the UI. Pretty quick/easy solution. I wanted to mention it here.

See also: development smtp server for windows

Upvotes: 3

Enzero
Enzero

Reputation: 1141

I found this - http://improve.dk/archive/2010/07/01/papercut-vs-smtp4dev-testing-mail-sending-locally.aspx which explain how to use papercut and smtp4dev which are both good tools

Upvotes: 3

Adam Butler
Adam Butler

Reputation: 3037

As per many of the other suggestions a free tool I've used quite a lot: http://www.toolheap.com/test-mail-server-tool/

Not really for TDD but useful in manual testing as it can pop up an outlook express window with each email that would be sent.

Upvotes: 0

Pete
Pete

Reputation: 431

Note that the SmtpClientWrapper class proposed by tvanfosson needs the all-important "virtual" keyword in its declaration of the Send method, otherwise you are back in the same boat as trying to Mock the SmtpClient directly.

Upvotes: 0

Carlos Mendible
Carlos Mendible

Reputation: 331

You can also use netDumbster.

http://netdumbster.codeplex.com/

Upvotes: 3

argatxa
argatxa

Reputation: 520

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>

Upvotes: 11

S&#233;bastien Gruhier
S&#233;bastien Gruhier

Reputation: 644

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

Upvotes: 2

Anthony Johnston
Anthony Johnston

Reputation: 9584

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

Upvotes: 0

Don Kirkby
Don Kirkby

Reputation: 56660

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.

Upvotes: 2

Related Questions