gpt4 book ai didi

c# - 尝试连接到 "smtp.office365.com"时出现 MailKit.Security.SslHandshakeException

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:39 35 4
gpt4 key购买 nike

我正在尝试使用 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);
}

我从 this comment 得到的 SaslMechanismNtlmIntegrated 类在 GitHub 上。

每当我运行这段代码时,我都会得到一个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详细信息中提到,但给出的解决方法并未解决问题。

最佳答案

willaien提到的解决方案解决了这个问题。即,使用 SecureSocketOptions.StartTls 而不是 SecureSocketOptions.SslOnConnect

关于c# - 尝试连接到 "smtp.office365.com"时出现 MailKit.Security.SslHandshakeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55287641/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com