Testing SMTP with .net

.NetTestingSmtp

.Net Problem Overview


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?

.Net Solutions


Solution 1 - .Net

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.

Solution 2 - .Net

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>

Solution 3 - .Net

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.

Solution 4 - .Net

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>

Solution 5 - .Net

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:

Solution 6 - .Net

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();
}

    

Solution 7 - .Net

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

Solution 8 - .Net

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

Solution 9 - .Net

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.

Solution 10 - .Net

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

Solution 11 - .Net

You can also use netDumbster.

http://netdumbster.codeplex.com/

Solution 12 - .Net

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/

Solution 13 - .Net

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

Solution 14 - .Net

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.

Solution 15 - .Net

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.

Solution 16 - .Net

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.

Solution 17 - .Net

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniasksillyquestionsView Question on Stackoverflow
Solution 1 - .NetSean CarpenterView Answer on Stackoverflow
Solution 2 - .NetLachlan RocheView Answer on Stackoverflow
Solution 3 - .NetDon KirkbyView Answer on Stackoverflow
Solution 4 - .NetargatxaView Answer on Stackoverflow
Solution 5 - .Netf3lixView Answer on Stackoverflow
Solution 6 - .NettvanfossonView Answer on Stackoverflow
Solution 7 - .NetEnzeroView Answer on Stackoverflow
Solution 8 - .NetSir CodesALotView Answer on Stackoverflow
Solution 9 - .NetDon KirkbyView Answer on Stackoverflow
Solution 10 - .NetSébastien GruhierView Answer on Stackoverflow
Solution 11 - .NetCarlos MendibleView Answer on Stackoverflow
Solution 12 - .NetBrad ParksView Answer on Stackoverflow
Solution 13 - .NetAnthony JohnstonView Answer on Stackoverflow
Solution 14 - .NetPeteView Answer on Stackoverflow
Solution 15 - .NetAdam ButlerView Answer on Stackoverflow
Solution 16 - .NettheDmiView Answer on Stackoverflow
Solution 17 - .NetAnish JohnView Answer on Stackoverflow