当我尝试连接到“smtp.office365.com”时,出现了MailKit.Security.SslHandshakeException错误。

16

我正在尝试使用MailKit通过"smtp.office365.com"发送电子邮件。 这是我正在使用的代码。

using (var client = new SmtpClient(new ProtocolLogger("smtp.log")))
{
    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
    client.Connect("smtp.office365.com", 587, SecureSocketOptions.SslOnConnect);
    client.Authenticate(new SaslMechanismNtlmIntegrated());
    Debug.WriteLine(client.IsAuthenticated);
    client.Send(message);
    client.Disconnect(true);
}

我从这个GitHub评论中获得了SaslMechanismNtlmIntegrated类。

每当我运行此代码时,都会收到一个SslHandshakeException

以下是异常的详细信息:

MailKit.Security.SslHandshakeException
  HResult=0x80131500
  Message=An error occurred while attempting to establish an SSL or TLS connection.

One possibility is that you are trying to connect to a port which does not support SSL/TLS.

The other possibility is 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. The certificate presented by the server is expired or invalid.

See https://github.com/jstedfast/MailKit/blob/master/FAQ.md#InvalidSslCertificate for possible solutions.
  Source=MailKit
  StackTrace:
   at MailKit.Net.Smtp.SmtpClient.<ConnectAsync>d__70.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MailKit.Net.Smtp.SmtpClient.Connect(String host, Int32 port, SecureSocketOptions options, CancellationToken cancellationToken)
   at QuoteAndOrder.SpecialsQuoteWindow.BtnEmail_Click(Object sender, RoutedEventArgs e) in SpecialsQuoteWindow.xaml.cs:line 501

Inner Exception 1:
IOException: The handshake failed due to an unexpected packet format.

我访问了详细信息中提到的FAQ,但是给出的解决方法并没有解决这个问题。


6
请尝试使用SecureSocketOptions.StartTls替代SecureSocketOptions.SslOnConnect - willaien
我已更新常见问题解答,以解释不同类型的SSL连接,因为人们经常对何时使用哪些选项感到困惑。 - jstedfast
对于其他人在使用面向.NET Core 2.0的传统应用程序时遇到此异常的情况,您可以通过在.NET Core 2.2.0或更高版本上运行应用程序来解决该问题(例如,通过运行dotnet --fx-version 2.2.0 .\YourAppNameHere.dll)。 - Eric Mutta
2个回答

23

提出解决此问题的方案是由willaien,使用SecureSocketOptions.StartTls而不是SecureSocketOptions.SslOnConnect


这解决了我的问题。我必须使用端口587而不是25。 - Chris Rosete
1
这给了我另一个异常:MailKit.Net.Imap.ImapProtocolException: 'IMAP服务器意外断开连接。' - Bedir Yilmaz

0
我花了几个小时来调试这个异常,但是 MailKit 的 FAQ 中提到的原因都没有帮助。
所以,我在这里为未来可能遇到这个错误并来到这里的谷歌用户提供一些建议。
背景
对于 TLS/SSL 相关的问题,.NET 在某种程度上使用操作系统的加密原语(Windows 上的 Schannel 和 Linux 上的 OpenSSL)。这些库可以配置为使用一组远程服务器不支持的 "密码套件"。
仔细查看 SslHandShakeException 的内部异常。
在我的情况下,我在 Linux 上遇到了一个 "SSL Handshake failed with OpenSSL error - SSL_ERROR_SSL" 错误。
假设你不能直接联系电子邮件服务器提供商并要求升级他们的密码套件,那么你可以这样做:
Linux 修复方法
要解决Linux上的这个错误,通常需要编辑/etc/openssl/openssl.cnf文件,并将CipherString SECLEVEL从"2"降低到"1"。
[system_default_sect]
CipherString = DEFAULT:@SECLEVEL=1

Windows修复

在Windows上,启用/禁用的密码套件由注册表控制,您可以通过谷歌搜索要更改的键和值。或者尝试一个名为"IISCrypto"的免费工具,这是每个人都用作此类问题的图形用户界面编辑器(我与IISCrypto没有关联,但在我使用Windows时经常使用它)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接