我正在尝试使用System.Net.Mail.SmtpClient类通过我公司的电子邮件服务器转发电子邮件。所有到邮件服务器的SMTP连接都必须是SSL,并且它使用自签名证书。这对于你只需要在警告对话框上点击ok就可以了,但是有谁知道让SmtpClient接受自签名证书的方法吗?
我计划在Windows Azure平台上使用此应用,因此我将无法将自签名证书安装为受信任的根目录。
发布于 2010-02-06 19:52:42
我的问题最终是.Net SmtpClient类显然不支持使用端口465进行SMTP SSL连接。将端口25与自签名SSL证书一起使用可以正常工作。
MSDN System.Net论坛问题Can SmtpClient be configured to work with a self signed certificate?。
发布于 2010-01-28 18:38:46
您可以查看ServerCertificateValidationCallback属性:
ServicePointManager.ServerCertificateValidationCallback =
(sender, certificate, chain, sslPolicyErrors) => true;它表示一个回调,当运行时尝试验证SSL证书时将调用该回调。通过返回true,您基本上表示您不关心证书是否有效,或者不是->,您总是接受它。当然,在生产环境中使用自签名证书不是一个好主意。
发布于 2020-11-20 21:04:04
如果您想要更安全,您可能需要考虑执行以下操作:
theClient.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback =
(sender, certificate, chain, sslPolicyErrors) => {
if (sender == theClient) {
return true;
} else {
// you should apply the code from this SO answer
// https://stackoverflow.com/a/25895486/795690
// if you find that anything in your app uses this path
throw new ApplicationException("Certificate validation is currently disabled, extra code neeeded here!");
}
};在这段代码中,我们只为有问题的特定SMTP客户端自动批准证书;我们有一个存根代码路径,如果您发现应用程序中的其他任何东西正在使用它,您应该将其升级到explicitly reinstate default certificate validation。
另一种不同的、有用的方法是在this SO answer中仅在您实际需要的上下文中批准证书。
https://stackoverflow.com/questions/2153790
复制相似问题