建议使用smtp4dev作为mailkit客户端消息的服务器

时间:2016-01-06 16:12:41

标签: mailkit smtp4dev

我有一个控制台应用程序,我安装了mailkit包以用于消息传递。

我在main方法中有代码来测试mailkit smtp客户端。我有运行smtp4dev虚拟服务器,客户端代码是example code of mailkit in github,其中认证部分已注释,主机是localhost,端口26与smtp4dev配置匹配。

执行客户端代码时,smtp4dev stop running和未处理的异常发生者IOException: Unable to read data from the transport connection: an existing connection was forcibly closed by the remote host.

如何配置smtp4dev以从mailkit客户端接收消息?

2 个答案:

答案 0 :(得分:11)

经过一些反复试验,我能够通过以下安排获得成功。 smtp4dev options

我的代码与https://github.com/jstedfast/MailKit#sending-messages类似:

public void DoMail()
{
    var message = new MimeMessage();
    message.From.Add(new MailboxAddress("Joey", "joey@friends.com"));
    message.To.Add(new MailboxAddress("Alice", "alice@wonderland.com"));
    message.Subject = "How you doin?";

    message.Body = new TextPart("plain")
    {
        Text = @"Hey Alice,

What are you up to this weekend? Monica is throwing one of her parties on
Saturday and I was hoping you could make it.

Will you be my +1?

-- Joey
"
    };

    using (var client = new SmtpClient())
    {
        // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
        client.ServerCertificateValidationCallback = (s, c, h, e) => true;

        client.Connect("localhost", 25, false);

        // Note: since we don't have an OAuth2 token, disable
        // the XOAUTH2 authentication mechanism.
        client.AuthenticationMechanisms.Remove("XOAUTH2");

        // Note: only needed if the SMTP server requires authentication
        //client.Authenticate("joey", "password");

        client.Send(message);
        client.Disconnect(true);
    }
}

对于那些无法访问imgur的人:
域名:localhost
监听界面:0.0.0.0
港口号码:25(虽然在Dalsier的情况下,Dalsier会使用26) 扩展程序:

  • []隐式SSL / TLS
  • [x] 8BITMIME
  • [] STARTTLS
  • [] AUTH
  • [x] SIZE

SSL / TLS证书:
SSL / TLS证书密码:
最大消息大小(字节):0
接收超时(ms):30000
选项:

  • []需要身份验证
  • []需要安全连接
  • []仅允许通过安全连接进行明文身份验证

答案 1 :(得分:0)

你有client.Disconnect(true);吗?错误消息表明您没有。