【问题标题】:"The handshake failed due to an unexpected packet format" with SmtpClient使用 SmtpClient 的“由于意外的数据包格式导致握手失败”
【发布时间】:2020-04-22 22:47:28
【问题描述】:

我使用 SmtpClient 发送电子邮件,代码如下:

SmtpClient client = new SmtpClient("host", 587);
client.EnableSsl = true;

client.Credentials = new System.Net.NetworkCredential("user", "pass");

MailAddress from = new MailAddress("from_mail", String.Empty, System.Text.Encoding.UTF8);

MailAddress to = new MailAddress("to_mail");
MailMessage message = new MailMessage(from, to);
message.Body = "Say hello";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "Testing";
message.SubjectEncoding = System.Text.Encoding.UTF8;

client.Send(message);

如果我使用 .Net Framework 4.5,会报错:The handshake failed due to an unexpected packet format。 如果我使用 .Net Framework 4.6.1,它将成功发送邮件。

请给我推荐一下?

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    可能是 TLS 版本问题。

    如果在初始化 SMTP 客户端之前将 System.Net.ServicePointManager.SecurityProtocol 设置为使用 tls1.2,会发生什么情况?

    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    

    【讨论】:

    • 感谢 KristoferA!这个对我有用。邮件服务器位于客户端。也许它使用 TLS 1.2?
    • 这里的默认设置是SecurityProtocolType.Tls,将其更改为建议值后,问题自动消失了!
    • 这对我来说真是令人头疼,而且效果很好。得谢谢你。是一场噩梦。
    【解决方案2】:

    好吧,当我尝试连接到 pop3 电子邮件服务器时,我遇到了这个问题。我本应使用不安全的方式连接到 pop3 电子邮件服务器,然后轮询新电子邮件,但在不知不觉中启用了导致此问题的 SLL。

       Pop3Client _pop3Client = new Pop3Client();
      _pop3Client.Connect(_serverAddress, _serverPort, _isSslEnabled); // _isSslEnabled was true here 
    

    更改_isSslEnabled = false; 对我有用。要建立安全连接,您可能必须添加一行代码,如 KristoferA 在他的回答中提到的那样。

    【讨论】:

      猜你喜欢
      • 2017-12-25
      • 2021-10-27
      • 1970-01-01
      • 2011-07-07
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多