首页 技术 正文
技术 2022年11月15日
0 收藏 306 点赞 2,880 浏览 2852 个字

前段时间看文章了解到发邮件的SmtpClient已经过时了,微软官方推荐大家用其他解决方案,例如MailKit。

https://docs.microsoft.com/zh-cn/dotnet/api/system.net.mail.smtpclient?view=netcore-2.2

SmtpClient Class定义命名空间:System.Net.MailAssemblies:System.dll, netstandard.dll, System.Net.Mail.dll警告此 API 现已过时。允许应用程序使用简单邮件传输协议 (SMTP) 来发送电子邮件。C#复制[System.Obsolete("SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead")]public class SmtpClient : IDisposable

  

关于MailKit的使用,网上大把,轻而易举就搞定了,本机调试没问题,但是把服务端软件发布到Linux Docker运行,报错了

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.   at MailKit.Net.Smtp.SmtpClient.ConnectAsync(String host, Int32 port, SecureSocketOptions options, Boolean doAsync, CancellationToken cancellationToken)

  

报错信息里有一个连接,

https://github.com/jstedfast/MailKit/blob/master/FAQ.md#InvalidSslCertificate

查看介绍,已经按照要求去做了。

我用的是腾讯企业邮箱,Asp.Net Core服务器部署在阿里云,开始还以为两家的服务器不相容,后来发现在VMWare安装的CentOS虚拟机上测试同样报错,确定是Linux环境下的问题。于是开启百度模式,网上有很多说法,主要有几大类:

一,把client.ConnectAsync(“smtp.exmail.qq.com”, 465, SecureSocketOptions.Auto)连接邮件服务器的参数换一下

我把SecureSocketOptions的所有选项都换了一遍,都报错。

二,把连接邮件服务器端口从465改为587

改了仍然报错。

三,把帐号密码改为客户端专用密码

这个其实要先用微信绑定邮箱,然后,以前的登录密码作废了,需要使用客户端专用密码,不然连Foxmail客户端都无法收邮件了。但是我用了客户端专用密码,还是报错。

在stackoverflow上面看到有人遇到了同样的问题。

https://stackoverflow.com/questions/55013298/cryptographicexception-exception-when-setting-up-ssl-handshake-with-mailkit-usin

但是没有解决方案!作者把问题提交给微软了。

这么简单的一个功能,居然还有这么大的一个坑!真是蛋疼……

走投无路之际,到作者提交的这个问题下面看一看,

https://github.com/Microsoft/dotnet/issues/973

居然有一个湖南的同学也在说遇到了这个问题,怎么办?

作者的答复是:No – sorry we haven’t solved this. We worked around this by disabling the revocation check (property on the Malkit client).

突然看到了一线希望!可以禁用一个什么鬼属性,把这个问题绕过去。

在SmtpClient的属性里翻了一下,恩,应该就是它了,

client.CheckCertificateRevocation = false;

设置为false,测试一下,真的可以发邮件了!最后代码长这样

using (var client = new SmtpClient())
{
client.CheckCertificateRevocation = false;// For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
client.ServerCertificateValidationCallback = (s, c, h, e) => true;await client.ConnectAsync(“smtp.exmail.qq.com”, 465, SecureSocketOptions.Auto);// Note: only needed if the SMTP server requires authentication
//如果腾讯企业邮箱绑定了微信,需要把密码改为客户端专用密码
await client.AuthenticateAsync(Username, Password);await client.SendAsync(message);await client.DisconnectAsync(true);
}

  

顺带说一下,采用SecureSocketOptions.Auto参数,如果连接465端口,自动转换为SslOnConnect,如果连接587端口,自动转换为StartTlsWhenAvailable

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297