通过C#Mailkit/Mimekit发送电子邮件,但出现服务器证书错误 [英] Sending email via C# Mailkit / Mimekit but server certificate error comes in

查看:858
本文介绍了通过C#Mailkit/Mimekit发送电子邮件,但出现服务器证书错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio 2015中的0代码

0 Code in Visual Studio 2015

1我正在使用Mailkit最新版本(1.18.1.1)从我自己的电子邮件服务器发送电子邮件.

1 I am using Mailkit latest version (1.18.1.1) for sending an email from my own email server.

2电子邮件服务器具有自签名证书,该证书不是Trusted.

2 The email server is having a self signed certificate, which is not TRUSTED.

3我在代码中添加了以下两行,以忽略SERVER CERTIFICATE错误:

3 I have added both of the following lines in my code, to ignore the SERVER CERTIFICATE error:

client.ServerCertificateValidationCallback = (mysender, certificate, chain, sslPolicyErrors) => { return true; };
client.CheckCertificateRevocation = false;

4但是我的程序仍然崩溃.

4 But my program still crashes.

5在电子邮件服务器日志中,它显示错误:

5 In email server logs it shows the error:

来自未知[xxx.xxx.xxx.xxx]的SSL_accept错误:连接重置为 同行

SSL_accept error from unknown[xxx.xxx.xxx.xxx]: Connection reset by peer

我猜这是由于服务器证书问题而来的.因为在Wireshark捕获中,一获得SERVER证书,连接就会终止.

which I guess is coming because of the Server Certificate issue. Because in Wireshark capture, as soon as I get the SERVER certificate the connection is terminated.

6我还在系统中安装了电子邮件服务器的UNTRUSTED证书,但问题仍然存在.

6 I have also installed the UNTRUSTED certificate of email server in my system but still the problem persists.

7以下是错误的详细屏幕截图

7 Following is the detailed screenshot of error

8个完整代码:

使用(var client = new SmtpClient(new ProtocolLogger("logging.log")))

using (var client = new SmtpClient(new ProtocolLogger("logging.log")))

                    {

                        // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
                        client.ServerCertificateValidationCallback = (mysender, certificate, chain, sslPolicyErrors) => { return true; };
                        client.CheckCertificateRevocation = false;



                        client.Connect("xxx.com", 465, true);
                        // 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("xxx@xxx.com","123456");

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

推荐答案

如果控制连接的两端,则可能需要先检查不使用TLS的发送,以确保问题仅在使用TLS时发生.

If you control both ends of the connection, you might want to first check sending without TLS, to be sure the problem only happens when using TLS.

也请尝试运行没有线缆,提琴手或其他中间人的嗅探器/代理,以确保以安全的方式到达服务器没有问题.检查您的防病毒软件或Internet安全系统是否由于不受信任的证书而关闭了您的连接.

Also try running without wireshark, fiddler or other man-in-the-middle sniffers/proxies, to ensure there is not a problem reaching the server in a secure way. Check your antivirus or internet security system is not closing your connection because of the untrusted certificate.

您可能要确保的另一件事是,客户端和服务器共享相同的协议:我知道旧的TLS和SSL协议已被弃用,因此客户端和服务器之间可能没有通用协议

Another thing you might want to ensure is that both your client and your server share the same protocols: I know older TLS and SSL protocols have become deprecated, so it is possible that there is no common protocol between the client and the server.

您还可以尝试启用system.net跟踪(从.NET 2.0开始可用),并查看是否从(非常详细的)日志中获得了一些更具体的见解: https://blogs.msdn.microsoft .com/dgorti/2005/09/18/using-system-net-tracing/

You can also try enabling system.net tracing (available since .NET 2.0) and see if you get some more specific insight from the (very detailed) logs you get: https://blogs.msdn.microsoft.com/dgorti/2005/09/18/using-system-net-tracing/

System.Net跟踪为 1)每个流程 2)显示线程 3)适用于SSL 4)适用于环回. 5)您不需要重新编译代码

System.Net tracing is 1) Per process 2) Shows threads 3) Works for SSL 4) Works for Loopback. 5) You don't need to recompile the code

您的问题似乎太宽泛了,我无法猜测问题,请尝试缩小问题范围.例如:

Your question seems a little too broad for me to guess the problem, please try narrowing down the problem.. For instance:

  • 尝试不使用TLS进行连接;
  • 尝试连接到另一台SMTP服务器(使用您知道标准邮件客户端可以连接到的服务器);
  • 尝试使用其他客户端连接到您的服务器(例如,使用雷鸟.)
  • 尝试在同一台计算机上同时运行客户端和服务器;
  • 在干净的虚拟机上尝试同样的方法

顺便说一下,SSPI似乎与受信任的安全问题有关,因此还要仔细检查一下,您还没有将服务器配置为仅接受受信任的用户.

By the way SSPI seems to be related to trusted security issues, so also double-check you don't have configured your server to only accept trusted users..

[/编辑]

如果这还不够,我会尝试更新答案=)

I will try to update my answer if this is not enough =)

HTH

这篇关于通过C#Mailkit/Mimekit发送电子邮件,但出现服务器证书错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆