使用 .net 测试 SMTP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/550887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:08:28  来源:igfitidea点击:

Testing SMTP with .net

.nettestingsmtp

提问by iasksillyquestions

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

我需要配置一个 SMTP 服务器来测试我发送电子邮件的网站(用于注册确认等)。

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?

有人可以推荐易于配置的SMTP服务器吗?

回答by Sean Carpenter

There's also Papercutwhich 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.

还有Papercut是一个 SMTP 服务器,它将接收消息但不会将它们传送到任何地方(允许您确保它们被正确发送)。接收到的消息在一个小的 GUI 中可见,并且也被写入一个目录。

回答by Lachlan Roche

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

在 .NET 中,可以将 SmtpClient 配置为通过将其放置在拾取目录中来发送电子邮件。

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

SmtpClient 的默认构造函数从 app.config 获取其设置,因此对于测试环境,我们可以如下配置它。

<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

MSDN 参考 - app.config mailSettings 元素http://msdn.microsoft.com/en-us/library/w355a94k.aspx

回答by Don Kirkby

The smtp4devproject 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.

smtp4dev项目是另一个虚拟SMTP服务器。我喜欢它,因为它有一个漂亮、简单的 UI,可以记录消息并让您查看最近消息的内容。使用 MSI 安装程序用 C# 编写。源代码可用。

回答by argatxa

For .NET guys out there. Keeping it simple.

对于那里的 .NET 人员。保持简单。

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>

回答by f3lix

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

我认为博客文章A Simple SMTP Server Mock for .NET 为您提供了您所需要的:一个 SMTP 服务器模拟

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

SMTP 服务器模拟基本上是一个伪造的 SMTP 服务器,可用于对发送电子邮件的应用程序进行单元测试。

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

此外,谷歌搜索 smtp 模拟服务器将为您提供用于测试目的的 SMTP 服务器选择。喜欢:

回答by tvanfosson

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.

另一种方法是在实现相同接口的 SmtpClient 周围创建一个包装器。然后在类中注入并使用包装器。在进行单元测试时,您可以替换一个对方法调用和响应有预期的模拟包装器。

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.

编辑:需要包装器(至少对于 RhinoMocks),因为 SmtpClient 不是从接口派生的,也没有虚拟方法。如果使用可以直接模拟没有虚方法的类的模拟框架,则可以跳过包装器并直接注入 SmtpClient 模拟。

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:

测试(使用 RhinoMocks)为:

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

回答by Enzero

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

我发现了这个 - http://improve.dk/archive/2010/07/01/papercut-vs-smtp4dev-testing-mail-sending-locally.aspx解释了如何使用 papercut 和 smtp4dev 这两个很好的工具

回答by Micah

I use Antix SMTP Server For Developerswhich 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.

我使用Antix SMTP Server For Developers,它就像打开一个应用程序一样简单。它将消息存储在一个文件夹中,您可以使用 UI 查看它们。非常快速/简单的解决方案。我想在这里提一下。

See also: development smtp server for windows

另请参阅:windows 开发 smtp 服务器

回答by Sébastien Gruhier

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

如果您使用的是 Mac OS X,则可以使用MockSMTP.app

回答by Don Kirkby

The DevNull SMTP serverlogs 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.

DevNull SMTP服务器会记录所有有关客户端和SMTP服务器之间的通信的血淋淋的细节。如果您试图诊断为什么您的发送代码不起作用,看起来它会很有用。

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

它是用 Java 编写的,并部署为一个可执行的 jar。源代码似乎不可用。