首页 文章

电子邮件服务器上的自签名证书

提问于
浏览
0

我正在使用测试email server在SSL环境中测试我的代码 . 我使用以下命令创建自签名根和签名证书 .

enter image description here

enter image description here

我用SmtpClient编写了一个简单的测试程序来发送测试邮件,代码如下:

System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(tFrom.Text, tTo.Text);

                mm.Subject = tSubject.Text;
                mm.Body = tBody.Text;

                SmtpClient client = new SmtpClient(tSmtp.Text, Convert.ToInt32(tPort.Text));
                client.UseDefaultCredentials = false;

                if (!String.IsNullOrEmpty(tPass.Text) && !String.IsNullOrEmpty(tUser.Text))
                {
                    client.EnableSsl = true;
                    client.Credentials = new NetworkCredential(tUser.Text, tPass.Text);
                }
                client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
                client.SendAsync(mm, null);

在我的服务器(Smtp4Dev)上,我将其设置为接受TLS连接

enter image description here

在我的客户端计算机上,我有以下内容:

enter image description here

然而,每当我发送电子邮件时,我都会在服务器日志中收到错误:

220 Ready to start TLS
 Session ended abnormally.
 System.NotSupportedException: The server mode SSL must use a certificate with the associated private key.

如果我在没有SSL的情况下尝试此操作,那么我可以收到我的电我尝试在个人和受信任的根证书下添加证书,但仍然会出现相同的错误 . 我是SSL的新手,所以我希望你能指出我正确的方向 .

1 回答

  • 0

    看看实现 ServerCertificateValidationCallback 委托 . 在使用 SmtpClient 之前尝试这样的事情:

    System.Net.ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };
    

    返回true并因此接受所有证书在开发环境中只是一个好主意,但您可以扩展委托实现以检查其他属性,而不是盲目地为所有内容返回true .

相关问题