问题描述
下面的函数工作在一个控制台应用程序,但它不是我的asp.net网站工作。我得到一个错误:
远程证书按验证程序是无效的。
公共静态无效SendEmail(毫米MAILMESSAGE)
{
SmtpClient SMTP =新SmtpClient();
smtp.Host =smtp.gmail.com;
smtp.Port = 587;
smtp.EnableSsl = TRUE;
smtp.Credentials =新的NetworkCredential([email protected],通行证); smtp.Send(毫米);
}
您可以尝试处理证书验证事件,使其更容易,以确定为什么远程证书被认为是无效的原因。对于这一点,调用SendEmail方法添加以下行之前:
ServicePointManager.ServerCertificateValidationCallback =新RemoteCertificateValidationCallback(ValidateServerCertificate);
然后提供ValidateServerCertificate实现,像
公共BOOL ValidateServerCertificate(对象发件人,X509证书证书,X509Chain链,SslPolicyErrors sslPolicyErrors)
{
//使用适当的验证取代
如果(sslPolicyErrors == SslPolicyErrors.None)
返回true;
其他
返回false;
}
由ValidateServerCertificate所收到的参数应该给你关于为什么验证失败的详细信息(检查sslPolicyErrors)。您也可以访问证书颁发机构的远程证书和链。
一种可能的情况是,你在不同的用户比控制台应用程序下的网站,该用户不信任Gmail的证书颁发者(或链中的一个中介机构)。如果你莫名其妙地从本地机器/受信任的根在Windows证书存储中的证书删除证书可能发生这种情况。
The function below works in a console application but it's not working in my asp.net web site. I am getting an error:The remote certificate is invalid according to the validation procedure.
public static void SendEmail(MailMessage mm)
{
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("[email protected]", "Pass");
smtp.Send(mm);
}
You could try to handle the certificate validation event to make it easier to determine the reason why the remote certificate is considered invalid. For this, before calling the SendEmail method add the following line:
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
and then provide an implementation for ValidateServerCertificate, like
public bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
// replace with proper validation
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
else
return false;
}
The parameters received by ValidateServerCertificate should give you details about why the validation fails (check sslPolicyErrors). You also have access to the remote certificate and chain of certificate authorities.
One possible scenario is that you run the website under a different user than the console application, and this user does not trust the issuer (or a intermediary authority in the chain) of the gmail's certificate. This might happen if you somehow have deleted certificates from the Local Machine/Trusted Root Certificates in Windows Certificate Store.
这篇关于远程证书根据验证过程是无效的。 GMAIL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!