首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mailkit IMapClient没有击中ServerCertificateValidationCallback & SslHandshakeException

Mailkit IMapClient没有击中ServerCertificateValidationCallback & SslHandshakeException
EN

Stack Overflow用户
提问于 2021-05-26 14:00:15
回答 1查看 347关注 0票数 0

我试图使用Windows server 2019计算机上的以下代码连接到IMAP服务器:

代码语言:javascript
复制
using (var client = new ImapClient(new ProtocolLogger("protocol.log")))
 {
        var address = EnvReader.GetStringValue("EMAIL_ADDRESS");
        var password = EnvReader.GetStringValue("EMAIL_PASSWORD");
        var creds = new NetworkCredential(address, password);

        client.CheckCertificateRevocation = false;
        client.ServerCertificateValidationCallback = (s, c, h, e) =>
        {
          Console.WriteLine("ALL UP IN THIS CALLBACK" + e.ToString());
          return true;
        };
        client.Connect("outlook.office365.com", 993, SecureSocketOptions.SslOnConnect);
        client.Authenticate(address, password);
 }

在我的Mac上,这段代码运行得非常好,我可以连接并随后验证。

在Windows机器上,我收到以下异常:

代码语言:javascript
复制
MailKit.Security.SslHandshakeException: An error occurred while attempting to establish an SSL or TLS connection.
This usually means that the SSL certificate presented by the server is not trusted by the system for one or more of
the following reasons:
1. The server is using a self-signed certificate which cannot be verified.
2. The local system is missing a Root or Intermediate certificate needed to verify the server's certificate.
3. A Certificate Authority CRL server for one or more of the certificates in the chain is temporarily unavailable.
4. The certificate presented by the server is expired or invalid.
5. The set of SSL/TLS protocols supported by the client and server do not match.
6. You are trying to connect to a port which does not support SSL/TLS.

See https://github.com/jstedfast/MailKit/blob/master/FAQ.md#SslHandshakeException for possible solutions

基于链接FAQ中的信息,我添加了ServerCertificateValidationCallback,但是回调从未命中(前面提到的异常仍然抛出,相关的控制台日志记录永远不会发生,回调中的断点也不会在调试时命中)。

从我的阅读来看,ServerCertificateValidationCallback应该处理异常消息提到的第1-4种情况。我可以在Mac上指定的端口上连接,这似乎排除了第6种情况(我还尝试了端口143 + SecureSocketOptions.StartTls)。但是,我找不到任何信息表明Windows 2019无法处理SSL/TSL协议。

任何关于( a)处理此异常和(或) b)为什么ServerCertificateValidationCallback不触发的想法都将不胜感激。

编辑:我的项目正在引用.NET 5.0

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-26 18:40:51

让我们来看看每一种可能性:

  1. 服务器正在使用无法验证的自签名证书.

Office365.com不会使用自签名证书,因此在这种情况下不会出现问题。

本地系统缺少验证服务器的certificate.所需的根或中间证书

这是非常可能的,但是ServerCertificateValidationCallback覆盖应该覆盖这个失败。但是,它没有被击中..。所以它实际上并没有绕过这个潜在的错误。

用于链中一个或多个证书的证书颁发机构CRL服务器临时为unavailable.。

这将被client.CheckCertificateRevocation = false;否定

  1. 服务器提供的证书过期或无效。

情况并非如此,因为证书要到2022年1月21日才到期。

  1. 客户端和服务器支持的SSL/TLS协议集不匹配。

服务器至少支持TLSv1.2,这是MailKit在所有目标框架版本(.NET 4.5 -> 5.0 +netStandard2.x)中支持的默认TLS协议版本。

  1. 您正试图连接到不支持SSL/TLS.

的端口。

端口993是正确的端口,SslOnConnect是正确的选项,所以这不是问题。

假设MailKit的SslStream.AuthenticateAsClientAsync()调用中没有通过验证回调方法(.NET 5.0与其他版本不同)中的错误,那么InnerException是什么?也许这会提供一些启示。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67706522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档