2016-01-06 79 views
6

我有一个控制台应用程序,并且我为消息传递目的安装了mailkit包。推荐使用smtp4dev作为邮件客户端消息服务器的方式

我在测试mailkit smtp客户端的主要方法中有代码。我有smtp4dev虚拟服务器运行,客户端代码是example code of mailkit in github,身份验证部分评论,主机是本地主机和端口26,匹配smtp4dev配置。

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

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

回答

0

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

+1

是的,我做。主要的问题是smtp4dev正在侦听端口26,并且客户端代码运行smtp4dev时停止。这似乎是在smpt4dev代码中抛出的异常。 – Dalsier

8

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

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

public void DoMail() 
{ 
    var message = new MimeMessage(); 
    message.From.Add(new MailboxAddress("Joey", "[email protected]")); 
    message.To.Add(new MailboxAddress("Alice", "[email protected]")); 
    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:
域名:本地主机
听接口:0.0.0.0
端口号:25(虽然,在Dalsier的情况下,Dalsier会使用26)
扩展:

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

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

  • []需要验证
  • []要求安全连接
  • []只允许通过安全连接明文认证
+1

这条线有所不同:client.ServerCertificateValidationCallback =(s,c,h,e)=> true;即使使用上面的所有配置,如果没有,仍然会失败。 – jpgrassi

相关问题