C# “根据验证程序,远程证书无效。” 使用 Gmail SMTP 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/777607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"The remote certificate is invalid according to the validation procedure." using Gmail SMTP server
提问by Josh
I'm getting this error:
我收到此错误:
The remote certificate is invalid according to the validation procedure.
根据验证程序,远程证书无效。
whenever I try to send e-mail using Gmail's SMTP server in my C# code. Can someone point me to the right direction for a solution to this problem?
每当我尝试在我的 C# 代码中使用 Gmail 的 SMTP 服务器发送电子邮件时。有人可以指出我解决这个问题的正确方向吗?
The following is the stack trace...
以下是堆栈跟踪...
at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
at System.Net.TlsStream.CallProcessAuthentication(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Mail.SmtpConnection.Flush()
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at BulkEmail.frmemail.mailsending(String toaddress, String fromaddress, String fromname, String subject, String pwd, String attachements, String mailmessage, String htmlmessage, Int32 i, Int32 j, String replytoaddress)
采纳答案by Yury Skaletskiy
Warning: Do not use this in production code!
警告:不要在生产代码中使用它!
As a workaround, you can switch off certificate validation. Only ever do this to obtain confirmation that the error is being throw because of a bad certificate.
作为解决方法,您可以关闭证书验证。只有这样做才能确认由于证书错误而引发错误。
Call this method before you call smtpclient.Send()
:
在调用之前调用此方法smtpclient.Send()
:
[Obsolete("Do not use this in Production code!!!",true)]
static void NEVER_EAT_POISON_Disable_CertificateValidation()
{
// Disabling certificate validation can expose you to a man-in-the-middle attack
// which may allow your encrypted message to be read by an attacker
// https://stackoverflow.com/a/14907718/740639
ServicePointManager.ServerCertificateValidationCallback =
delegate (
object s,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors
) {
return true;
};
}
回答by LinuxLover
You can improve the code by asking the user when the certificate is not valid whether he wants to continue or not. Do you want to continue?As below:
您可以通过在证书无效时询问用户是否要继续来改进代码。你要继续吗?如下:
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(ValidateServerCertificate);
And add a method like this:
并添加一个这样的方法:
public static bool ValidateServerCertificate(object sender,X509Certificate certificate,X509Chain chain,SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
else
{
if (System.Windows.Forms.MessageBox.Show("The server certificate is not valid.\nAccept?", "Certificate Validation", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
return true;
else
return false;
}
}
回答by Frans
Your website folder needs network service security. Especially the web.config. It uses this account to access your registry for the certificates. This will stop the need to add a hack to your code.
您的网站文件夹需要网络服务安全。尤其是 web.config。它使用此帐户访问您的证书注册表。这将不再需要向您的代码添加 hack。
回答by NullPointer
For those encountering this same error when connecting to a local site with a self-signed certificate, the following blog post helped me out.
对于使用自签名证书连接到本地站点时遇到相同错误的人,以下博客文章帮助了我。
http://brainof-dave.blogspot.com.au/2008/08/remote-certificate-is-invalid-according.html
http://brainof-dave.blogspot.com.au/2008/08/remote-certificate-is-invalid-according.html
回答by Pawel Lesnikowski
Are you sure you are using correct SMTP server address?
您确定您使用的是正确的 SMTP 服务器地址吗?
Both smtp.google.com and smtp.gmail.com work, but SSL certificate is issued to the second one.
smtp.google.com 和 smtp.gmail.com 都可以工作,但 SSL 证书颁发给第二个。
回答by Chitra Pannirselvam
Get the same error while sending from outlook because of ssl. Tried setting EnableSSL = false resolved the issue.
由于 ssl,从 Outlook 发送时出现相同的错误。尝试设置 EnableSSL = false 解决了该问题。
example:
例子:
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("[email protected]", "xxxxx")
};
回答by T-Rex
The link here solved my problem.
这里的链接解决了我的问题。
http://brainof-dave.blogspot.com.au/2008/08/remote-certificate-is-invalid-according.html
http://brainof-dave.blogspot.com.au/2008/08/remote-certificate-is-invalid-according.html
I went to url of the web service (on the server that had the issue), clicked on the little security icon in IE, which brought up the certificate. I then clicked on the Details tab, clicked the Copy To File button, which allowed me to export the certifcate as a .cer file. Once I had the certificate locally, I was able to import it into the certificate store on the server using the below instructions.
我访问了 Web 服务的 url(在有问题的服务器上),点击了 IE 中的小安全图标,它显示了证书。然后我单击“详细信息”选项卡,单击“复制到文件”按钮,这允许我将证书导出为 .cer 文件。在本地获得证书后,我可以使用以下说明将其导入服务器上的证书存储区。
Start a new MMC. File --> Add/Remove Snap-In... Click Add... Choose Certificates and click Add. Check the "Computer Account" radio button. Click Next.
启动一个新的 MMC。文件 --> 添加/删除管理单元... 单击添加... 选择证书并单击添加。选中“计算机帐户”单选按钮。点击下一步。
Choose the client computer in the next screen. Click Finish. Click Close. Click OK. NOW install the certificate into the Trusted Root Certification Authorities certificate store. This will allow all users to trust the certificate.
在下一个屏幕中选择客户端计算机。单击完成。单击关闭。单击确定。现在将证书安装到受信任的根证书颁发机构证书库中。这将允许所有用户信任该证书。
回答by David Burg
There is an MSDN blog article on investigating this type of issues:
有一篇关于调查此类问题的 MSDN 博客文章:
Troubleshooting ASP.NET – The remote certificate is invalid according to the validation procedure:
http://blogs.msdn.com/b/jpsanders/archive/2009/09/16/troubleshooting-asp-net-the-remote-certificate-is-invalid-according-to-the-validation-procedure.aspx
ASP.NET 故障排除 - 根据验证程序,远程证书无效:http:
//blogs.msdn.com/b/jpsanders/archive/2009/09/16/troubleshooting-asp-net-the-remote-certificate- is-invalid-according-to-the-validation-procedure.aspx
回答by AliSafari186
Check your computer's Date and Time. If it is wrong, update it to the current time or set it automatically to get the time from the Internet.
检查计算机的日期和时间。如果错误,将其更新为当前时间或自动设置为从互联网获取时间。
Because certificates are tied to a fixed time period, if your clock is wrong, you are likely to get errors like this. In that scenario, by fixing the time, the problem will be fixed.
由于证书与固定时间段相关联,如果您的时钟错误,您很可能会遇到这样的错误。在这种情况下,通过固定时间,问题将得到解决。
回答by Josh
My issue was not that I was referencing the server by the IP address instead of the URL. I had purchased a signed certificate from a CA for use inside a private network. The URL specified on the certificate does matter when referencing the server. Once I referenced the server by the URL in the certificate everything started to work.
我的问题不是我通过 IP 地址而不是 URL 来引用服务器。我从 CA 购买了一个签名证书,用于在专用网络中使用。在引用服务器时,证书上指定的 URL 很重要。一旦我通过证书中的 URL 引用了服务器,一切就开始工作了。