MailKit在使用LetsEncrypt SSL证书时,会得到一个SslHandshakeException。

问题描述 投票:0回答:2

我有一台服务器(Centos 7)被设置为邮件服务器。使用postfixdovecotopendkimopendmarc。它的工作原理是,用户能够使用gmail连接他们的电子邮件,例如。能够发送和接收邮件。

此外,当我使用MailKit和测试我的.NET Core应用程序从我的家庭PC 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)

我的MailKit(在服务器外可以正常工作)。

using (SmtpClient emailClient = new SmtpClient())
{
    await emailClient.ConnectAsync(emailConfiguration.SmtpServer, emailConfiguration.SmtpPort, SecureSocketOptions.StartTls);
    emailClient.AuthenticationMechanisms.Remove("XOAUTH2");  

    await emailClient.AuthenticateAsync(emailConfiguration.SmtpUsername, emailConfiguration.SmtpPassword);
    await emailClient.SendAsync(message);

    await emailClient.DisconnectAsync(true);
}

编辑:MailKit的异常(证书是正确的,不是自签的)。

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.
centos postfix-mta mailkit dovecot
2个回答
0
投票

unable to get certificate CRL 错误听起来像是SslStream无法获得CRL,可能是因为CRL服务器因某种原因无法到达。

你可以尝试在SslStream中加入 emailClient.CheckCertificateRevocation = false; 前的ConnectAsync来检查是否是这个问题。

另一个错误。unable to get local issuer certificate可能是因为MailKit运行的服务器的X509Store中没有Root CA证书,而你的家用电脑却有。

更新。

问题是LetsEncrypt SSL证书不包含CRL位置,这意味着证书撤销检查会失败。

要绕过这个问题,你需要设置 client.CheckCertificateRevocation = false; 在连接之前,我找到了一个可行的答案,但不是我喜欢的方法。


0
投票

我找到了一个可行的答案,但并不是我喜欢的方法,因为我希望能够使用MailKit来做更多的事情,而不仅仅是我自己的服务器(在应用程序中进行配置)。

我之所以来解决这个问题,是因为我认为这与内部流量出了问题有关。

通过使用旧的 Smtp客户端System.Net.Mail 我能够使用 默认凭证.

using (SmtpClient client = new SmtpClient("127.0.0.1"))
{
    client.UseDefaultCredentials = true;

    MailAddress from = new MailAddress(emailMessage.FromAddress.Address, emailMessage.FromAddress.Name);

    foreach (IEmailAddress emailAddress in emailMessage.ToAddresses)
    {
        MailAddress to = new MailAddress(emailAddress.Address, emailAddress.Name);

        MailMessage email = new MailMessage(from, to)
        {
            Subject = emailMessage.Subject,
            Body = emailMessage.Content
        };

        await client.SendMailAsync(email);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.