首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何修复“远程证书根据验证过程无效”

如何修复“远程证书根据验证过程无效”
EN

Stack Overflow用户
提问于 2022-05-12 07:29:50
回答 1查看 3.8K关注 0票数 0

我将尝试通过Mailkit发送电子邮件,但是遇到了来自"System.Security.Authentication.AuthenticationException“的错误,即”根据验证过程远程证书无效“(从丹麦语翻译),我的邮件服务器运行SSL,TLS支持版本1.2和1.3。我的代码如下:我不希望它是太多的代码,但我不知道在哪里增强代码,以便它能够正确地处理SSL :-(

错误发生在行"client.Connect("servername",587,true)中;

所以我的问题是:如何通过Mailkit来避免这个错误消息?

代码语言:javascript
复制
public void SendMail(string AFromMailAdr, string AFromName, string AToMailAdr, string AToName, string ASubject, string ABody)
{
    MimeMessage message = new MimeMessage();
    ...
    using (var client = new MailKit.Net.Smtp.SmtpClient())
    {
        client.Timeout = 30000;
        client.Connect("servername", 587, true);
        client.Authenticate("Username", "password");
        client.Send(message);
        client.Disconnect(true);
    }
}

我谷歌了很多到现在还没有找到正确的答案-因此,我恳请在这里这样问。

EN

Stack Overflow用户

回答已采纳

发布于 2022-05-12 07:54:49

为了公平起见,应该检查/纠正根本的问题。

您可以控制MailKit如何使用一个ServerCertificateValidationCallback进行服务器证书验证。

出于调试目的,您可以在回调函数中使用return true;

MailKit文档中的代码:

代码语言:javascript
复制
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
    // Set our custom SSL certificate validation callback.
    client.ServerCertificateValidationCallback = MySslCertificateValidationCallback;

    client.Timeout = 30000;
    client.Connect("servername", 587, true);
    client.Authenticate("Username", "password");
    client.Send(message);
    client.Disconnect(true);
}

    static bool MySslCertificateValidationCallback (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        // If there are no errors, then everything went smoothly.
        if (sslPolicyErrors == SslPolicyErrors.None)
            return true;

        // Note: MailKit will always pass the host name string as the `sender` argument.
        var host = (string) sender;

        if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNotAvailable) != 0) {
            // This means that the remote certificate is unavailable. Notify the user and return false.
            Console.WriteLine ("The SSL certificate was not available for {0}", host);
            return false;
        }

        if ((sslPolicyErrors & SslPolicyErrors.RemoteCertificateNameMismatch) != 0) {
            // This means that the server's SSL certificate did not match the host name that we are trying to connect to.
            var certificate2 = certificate as X509Certificate2;
            var cn = certificate2 != null ? certificate2.GetNameInfo (X509NameType.SimpleName, false) : certificate.Subject;

            Console.WriteLine ("The Common Name for the SSL certificate did not match {0}. Instead, it was {1}.", host, cn);
            return false;
        }

        // The only other errors left are chain errors.
        Console.WriteLine ("The SSL certificate for the server could not be validated for the following reasons:");

        // The first element's certificate will be the server's SSL certificate (and will match the `certificate` argument)
        // while the last element in the chain will typically either be the Root Certificate Authority's certificate -or- it
        // will be a non-authoritative self-signed certificate that the server admin created. 
        foreach (var element in chain.ChainElements) {
            // Each element in the chain will have its own status list. If the status list is empty, it means that the
            // certificate itself did not contain any errors.
            if (element.ChainElementStatus.Length == 0)
                continue;

            Console.WriteLine ("\u2022 {0}", element.Certificate.Subject);
            foreach (var error in element.ChainElementStatus) {
                // `error.StatusInformation` contains a human-readable error string while `error.Status` is the corresponding enum value.
                Console.WriteLine ("\t\u2022 {0}", error.StatusInformation);
            }
        }

        return false;
    }
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72211623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档