发送SMTP电子邮件时发送RemoteCertificateNameMismatch

时间:2017-09-06 07:04:30

标签: c# .net ssl smtp certificate

我正在尝试使用我们公司的Outlook使用EnableSSL = True发送电子邮件。但是,我现在无法使用下面的示例代码解决我遇到的问题:

public static bool RemoteServerCertificateValidationCallback(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        return sslPolicyErrors == SslPolicyErrors.None;
    }

    public static void SendMail()
    {
        try
        {
            ServicePointManager.ServerCertificateValidationCallback = RemoteServerCertificateValidationCallback;
            var mail = new MailMessage();
            using (var smtp = new SmtpClient("mail.abccompany.com"))
            {
                mail.From = new MailAddress("john.smith@abccompany.com");
                mail.To.Add("john.smith@abccompany.com");
                mail.Subject = "Test";
                mail.Body = "Test!";

                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.UseDefaultCredentials = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

                smtp.Send(mail);
                Console.WriteLine("Success");
            }


        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception: " + ex.Message);
        }
    }

SslPolicyErrors的值是“RemoteCertificateNameMismatch”,我不知道如何解决它。我尝试将证书值从“RemoteServerCertificateValidationCallback”方法导出到文件并将其安装在我的计算机中,但仍然无法解决问题。任何想法都会......

0 个答案:

没有答案