Centos MailKit获取带有LetsEncrypt SSL证书的SslHandshakeException

Centos MailKit获取带有LetsEncrypt SSL证书的SslHandshakeException,centos,postfix-mta,mailkit,dovecot,Centos,Postfix Mta,Mailkit,Dovecot,我有一个服务器(Centos 7)设置用作邮件服务器。使用postfix/dovecot/opendkim/opendmarc。。 它可以正常工作,例如,用户可以使用gmail连接他们的电子邮件。能够收发邮件 此外,当我使用MailKit并从家用pc测试.NET Core应用程序时,MailKit连接良好,电子邮件已发送 但是,当我将应用程序部署到服务器时,MailKit无法连接 如果我查看日志,我会看到以下内容 postfix/submission/smtpd[4486]: match_hos

我有一个服务器(Centos 7)设置用作邮件服务器。使用postfix/dovecot/opendkim/opendmarc。。 它可以正常工作,例如,用户可以使用gmail连接他们的电子邮件。能够收发邮件

此外,当我使用MailKit并从家用pc测试.NET Core应用程序时,MailKit连接良好,电子邮件已发送

但是,当我将应用程序部署到服务器时,MailKit无法连接

如果我查看日志,我会看到以下内容

postfix/submission/smtpd[4486]: match_hostname: unknown ~? 127.0.0.1/32
postfix/submission/smtpd[4486]: match_hostaddr: MY_SERVER_IP ~? 127.0.0.1/32
postfix/submission/smtpd[4486]: match_hostname: unknown ~? MY_SERVER_IP/32
postfix/submission/smtpd[4486]: match_hostaddr: MY_SERVER_IP  ~? MY_SERVER_IP/32
postfix/submission/smtpd[4486]: lost connection after STARTTLS from unknown[MY_SERVER_IP]
但是如果我在日志中看得更高一点

Anonymous TLS connection established from unknown[MY_SERVER_IP]: TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)
My MailKit(在服务器外部工作正常):

使用(SmtpClient-emailClient=new-SmtpClient())
{
等待emailClient.ConnectAsync(emailConfiguration.SmtpServer、emailConfiguration.SmtpPort、SecureSocketOptions.StartTls);
emailClient.AuthenticationMechaniss.Remove(“XOAUTH2”);
等待emailClient.AuthenticateAsync(emailConfiguration.SmtpUsername、emailConfiguration.SmtpPassword);
等待emailClient.SendAsync(消息);
等待emailClient.DisconnectAsync(true);
}
编辑: MailKit的例外情况(证书正确且未自签名):


无法获取证书CRL
错误听起来像是SslStream无法获取CRL,可能是由于某种原因无法访问CRL服务器

您可以尝试添加
emailClient.checkCertificatereJournal=false
在ConnectAsync之前检查这是否是问题所在

另一个错误,
无法获取本地颁发者证书
,可能是因为运行MailKit的服务器在其X509Store中没有根CA证书,但您的家用电脑有根CA证书

更新:

问题在于LetsEncrypt SSL证书不包含CRL位置,这意味着证书吊销检查将失败


要绕过此操作,需要设置
client.checkCertificateReshibition=false在连接之前。

我找到了一个答案,这个答案很有效,但不是我的首选方法,因为我希望能够使用MailKit实现更多功能,而不仅仅是我自己的服务器(使其可以在应用程序本身中进行配置)

我找到了解决方案,因为我认为这与一些内部通信出错有关

通过使用System.Net.Mail中旧的SmtpClient,我能够使用DefaultCredentials

使用(SmtpClient=new SmtpClient(“127.0.0.1”))
{
client.UseDefaultCredentials=true;
MailAddress from=新邮件地址(emailMessage.FromAddress.Address,emailMessage.FromAddress.Name);
foreach(emailMessage.ToAddress中的IEmailAddress emailAddress)
{
MailAddress to=新邮件地址(emailAddress.Address,emailAddress.Name);
MailMessage email=新的MailMessage(从,到)
{
Subject=emailMessage.Subject,
Body=emailMessage.Content
};
等待client.SendMailAsync(电子邮件);
}
}
我在使用.NET core 3.1的ubuntu 20.04上也有同样的问题 经过3个小时的反复试验,我终于找到了解决方案
我刚刚忽略了
证书验证回调

使用var-client=new-SmtpClient(new-ProtocolLogger(“smtp.log”);
client.checkCertificateReliation=false;
client.ServerCertificateValidationCallback=(发送方、证书、链、错误)=>true;
client.Connect(“your.smtp.host”,587,SecureSocketOptions.StartTls);

我希望这会有帮助:)

FWIW,
emailClient.AuthenticationMechanisms.Remove(“XOAUTH2”)从MailKit 2.0汉克斯开始就没有必要了,我不知道!谢谢你的回复!我的服务器上的证书是用Letsencrypt的certbot安装的,它应该很好,因为我没有浏览器(和gmail)抱怨。也许值得一试把它关掉。然而,我认为,因为MailKit试图从内部连接到同一台服务器。。防火墙/权限出现问题。我还没有试过你的答案,但找到了解决办法。请参阅我的答案。好的,问题是LetsEncrypt SSL证书不包含CRL服务器位置,这会导致SslStream无法验证它们。您需要指定
client.checkCertificateRetailation=false啊,好的,我现在明白了。我不知道。因此,关闭该选项是有意义的。谢谢
MailKit.Security.SslHandshakeException: An error occurred while attempting to establish an SSL or TLS connection.
May 19 16:07:37 domain.com NETCoreApp[4452]: The server's SSL certificate could not be validated for the following reasons:
May 19 16:07:37 domain.com NETCoreApp[4452]: • The server certificate has the following errors:
May 19 16:07:37 domain.com NETCoreApp[4452]: • unable to get certificate CRL
May 19 16:07:37 domain.com NETCoreApp[4452]: • The root certificate has the following errors:
May 19 16:07:37 domain.com NETCoreApp[4452]: • unable to get certificate CRL
May 19 16:07:37 domain.com NETCoreApp[4452]: • unable to get local issuer certificate
May 19 16:07:37 domain.com NETCoreApp[4452]: ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.