Testing SMTP with .net

2019-01-12 23:14发布

问题:

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?

回答1:

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.



回答2:

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



回答3:

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.



回答4:

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>


回答5:

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:

  • Dumbster - Java fake SMTP server
  • nDumbster - .NET port of Dumbster


回答6:

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


回答7:

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



回答8:

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



回答9:

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.



回答10:

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



回答11:

You can also use netDumbster.

http://netdumbster.codeplex.com/



回答12:

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



回答13:

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.



回答14:

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.



回答15:

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/



回答16:

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.