Asp.Net Core采用MailKit部署到Linux Docker连接邮件服务器报错

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

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

SmtpClient Class

定义

命名空间:

System.Net.Mail

Assemblies:

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

原文地址:https://www.cnblogs.com/sunnytrudeau/p/10822470.html