博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Asp.Net Core采用MailKit部署到Linux Docker连接邮件服务器报错
阅读量:6858 次
发布时间:2019-06-26

本文共 3145 字,大约阅读时间需要 10 分钟。

前段时间看文章了解到发邮件的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

转载于:https://www.cnblogs.com/sunnytrudeau/p/10822470.html

你可能感兴趣的文章
2014年java软件project师面试题收集
查看>>
Java并发编程:Callable、Future和FutureTask
查看>>
这些老外的开源技术养活了很多国产软件
查看>>
svn简单介绍
查看>>
hbase region still in transition
查看>>
CSS Flex布局属性整理
查看>>
【struts2】中method={1}具体解释
查看>>
Android Studio 函数使用方法提示 快捷键
查看>>
构建自己的PHP框架--构建模版引擎(2)
查看>>
vue28-2.0-过滤器
查看>>
Cocos2d-x 多点触摸
查看>>
MySql按周/月/日分组统计数据的方法
查看>>
自定义控件_VIewPager显示多个Item
查看>>
2015年年尾总结
查看>>
UI组件之AdapterView及其子类(五)ListView组件和ListActivity
查看>>
Linux编程之select
查看>>
数据库表设计--备份记录的表设计优化
查看>>
小谈业务应用架构
查看>>
JWPlayer Uncaught Error: Invalid SRT file
查看>>
mysql使用GROUP BY分组实现取前N条记录的方法
查看>>