c# - "The handshake failed due to an unexpected packet format" with SmtpClient
问题描述
I use SmtpClient to send email as below code:
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);
If I use .Net Framework 4.5, it will raise the error:The handshake failed due to an unexpected packet format. If I use .Net Framework 4.6.1, it will send mail successfully.
Please give me recommandation?
解决方案
可能是 TLS 版本问题。
如果在初始化 SMTP 客户端之前将 System.Net.ServicePointManager.SecurityProtocol 设置为使用 tls1.2,会发生什么?
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
推荐阅读
- time - 建立模型的算法执行时间
- html - 如何在 HTML 和 CSS 中制作幻灯片内容?
- bash - 如何在 linux bash 中睡不到一秒
- syntax - Erlang中基于arity的指定方法
- firebase - Fire Storage Exception([firebase_storage/unauthorized] 用户无权执行所需的操作。)(我的规则是允许读/写)
- amazon-web-services - 亚马逊 S3 网络服务器?
- javascript - 如何在 Java Script/TestCafe 中获取多个键值对数组
- css - css动画期间奇怪的拖尾效果
- python - 如何在不对结果中的日期时间分组的情况下对 Pandas 数据框进行重新采样
- nginx - Nginx lua openresty 变量作用域