随笔 - 59  文章 - 20  评论 - 61  阅读 - 85494

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

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

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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运行,报错了

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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,测试一下,真的可以发邮件了!最后代码长这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

posted on   SunnyTrudeau  阅读(1181)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示