C# I';当尝试连接到“时,我收到MailKit.Security.SslHandshakeException”;smtp.office365.com“;

C# I';当尝试连接到“时,我收到MailKit.Security.SslHandshakeException”;smtp.office365.com“;,c#,email,ssl,mailkit,C#,Email,Ssl,Mailkit,我正在尝试使用MailKit通过“smtp.office365.com”发送电子邮件。这是我正在使用的代码 使用(var-client=new-SmtpClient(new-ProtocolLogger(“smtp.log”)) { client.ServerCertificateValidationCallback=(s、c、h、e)=>true; client.Connect(“smtp.office365.com”,587,SecureSocketOptions.SslOnConnect)

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

使用(var-client=new-SmtpClient(new-ProtocolLogger(“smtp.log”))
{
client.ServerCertificateValidationCallback=(s、c、h、e)=>true;
client.Connect(“smtp.office365.com”,587,SecureSocketOptions.SslOnConnect);
Authenticate(新SaslMechanismNtlmIntegrated());
Debug.WriteLine(client.IsAuthenticated);
客户端。发送(消息);
client.Disconnect(true);
}
我从GitHub上获得的
saslmechanismntlimintegrated

每当我运行此代码时,都会得到一个
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.
MailKit.Security.SslHandshakeException
HResult=0x80131500
Message=尝试建立SSL或TLS连接时出错。
一种可能性是您正试图连接到不支持SSL/TLS的端口。
另一种可能性是,由于以下一个或多个原因,服务器提供的SSL证书不受系统信任:
1.服务器正在使用无法验证的自签名证书。
2.本地系统缺少验证服务器证书所需的根证书或中间证书。
3.服务器提供的证书已过期或无效。
看见https://github.com/jstedfast/MailKit/blob/master/FAQ.md#InvalidSslCertificate 寻找可能的解决办法。
来源=邮箱
堆栈跟踪:
在MailKit.Net.Smtp.SmtpClient.d_u70.MoveNext()上
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
位于MailKit.Net.Smtp.SmtpClient.Connect(字符串主机、Int32端口、SecureSocketOptions选项、CancellationToken CancellationToken)
在QuoteAndOrder.SpecialsQuoteWindow.BtnEmail中,单击SpecialsQuoteWindow.xaml.cs:第501行中的(对象发送方,RoutedEventArgs e)
内部异常1:
IOException:由于意外的数据包格式,握手失败。

我转到了详细信息中提到的解决方案,但给出的解决方案没有解决问题。

所述解决方案解决了此问题。即,使用
SecureSocketOptions.StartTls
而不是
SecureSocketOptions.SslOnConnect

尝试使用
SecureSocketOptions.StartTls
而不是
SecureSocketOptions.SslOnConnect
我已经更新了常见问题解答,以解释不同类型的SSL连接,因为人们总是对使用哪些选项感到困惑什么时候。这解决了我的问题。我不得不使用587端口而不是25端口。