Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C发送电子邮件时出现问题#_C#_Email_Smtp - Fatal编程技术网

C# 使用C发送电子邮件时出现问题#

C# 使用C发送电子邮件时出现问题#,c#,email,smtp,C#,Email,Smtp,当我尝试使用C#和gmail的smtp服务器发送电子邮件时,我遇到了这个错误 “根据验证过程,远程证书无效。” SSL已启用 使用的端口是587 使用的服务器名称是“Smtp.gmail.com” 用户名和密码正确 outlook express在具有相同设置的同一台pc上工作正常 c#程序在其他地方也可以正常工作……我们只有在客户端才会出现此错误 如有任何帮助,我将不胜感激 谢谢 编辑:@Andomar,在客户端的哪里可以找到根证书?我该如何解决这个问题 @Alnitak,我如何使用Syste

当我尝试使用C#和gmail的smtp服务器发送电子邮件时,我遇到了这个错误

“根据验证过程,远程证书无效。”

SSL已启用

使用的端口是587

使用的服务器名称是“Smtp.gmail.com”

用户名和密码正确

outlook express在具有相同设置的同一台pc上工作正常

c#程序在其他地方也可以正常工作……我们只有在客户端才会出现此错误

如有任何帮助,我将不胜感激

谢谢

编辑:@Andomar,在客户端的哪里可以找到根证书?我该如何解决这个问题

@Alnitak,我如何使用System.Net.Mail库发布starttls

@David,我应该将什么作为参数传递给“(对象发送者、X509证书证书、X509Chain链、SslPolicyErrors SslPolicyErrors)”


谢谢你,大卫。我已经添加了这些行。但是,我仍然对发生的事情感到困惑,因为据我所知,此代码与System.Net.Mail没有任何直接连接。希望问题消失。

检查客户端存储中是否有正确的根证书。并且客户端的系统日期正确。

检查客户端存储中是否有正确的根证书。并且客户端的系统日期是正确的。

您使用的是
STARTTLS
还是假设端口587上的连接从一开始就是SSL加密的


smtp.gmail.com
上的谷歌服务器需要STARTTLS。

您使用的是
STARTTLS
还是假设587端口上的连接从一开始就是SSL加密的


smtp.gmail.com
上的谷歌服务器需要STARTTLS。

还要检查根证书是否位于客户端的受信任根权限存储中。如果这是来自服务,那么将根证书添加到本地计算机存储也会有所帮助。为了更好地理解原因,我发现以下政策很有帮助

public bool ValidateServerCertificate( object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
// No errors so continue…
if (sslPolicyErrors == SslPolicyErrors.None)
       return true;

// I’m just logging it to a label on the page, 
//  this should be stored or logged to the event log at this time. 
lblStuff.Text += string.Format("Certificate error: {0} <BR/>", sslPolicyErrors);

// If the error is a Certificate Chain error then the problem is
// with the certificate chain so we need to investigate the chain 
// status for further info.  Further debug capturing could be done if
// required using the other attributes of the chain.
      if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors)
      {
       foreach (X509ChainStatus status in chain.ChainStatus)
       {
             lblStuff.Text += string.Format("Chain error: {0}: {1} <BR/>", status.Status, status.StatusInformation);
  }
}

// Do not allow this client to communicate 
//with unauthenticated servers.
      return false;
}

您也可以使用该策略完全删除错误,但解决问题比解决问题要好。

还要检查根证书是否位于客户端的受信任根权限存储中。如果这是来自服务,那么将根证书添加到本地计算机存储也会有所帮助。为了更好地理解原因,我发现以下政策很有帮助

public bool ValidateServerCertificate( object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
// No errors so continue…
if (sslPolicyErrors == SslPolicyErrors.None)
       return true;

// I’m just logging it to a label on the page, 
//  this should be stored or logged to the event log at this time. 
lblStuff.Text += string.Format("Certificate error: {0} <BR/>", sslPolicyErrors);

// If the error is a Certificate Chain error then the problem is
// with the certificate chain so we need to investigate the chain 
// status for further info.  Further debug capturing could be done if
// required using the other attributes of the chain.
      if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors)
      {
       foreach (X509ChainStatus status in chain.ChainStatus)
       {
             lblStuff.Text += string.Format("Chain error: {0}: {1} <BR/>", status.Status, status.StatusInformation);
  }
}

// Do not allow this client to communicate 
//with unauthenticated servers.
      return false;
}
您也可以使用该策略来完全删除错误,但解决问题比这样做要好。

您设置了吗

 client.EnableSsl = true;
因为gmail需要它,你设置了吗

 client.EnableSsl = true;


因为gmail需要它

在哪里检查证书等?根据我的C代码,我没有使用任何证书。我使用了System.Net.Mails中的类,因为它在别处工作,而不是客户端,我敢打赌客户端不知道您的CA,因此,您需要在客户端安装证书/CA是的,我知道SSL使用证书…但我认为System.Net.Mail库可以处理它们…如何在客户端安装证书?如何在客户端安装证书?如何检查?在哪里检查证书等?根据我的C代码,我没有使用任何证书。我使用了System.Net.Mails中的类,因为它在别处工作,而不是客户端,我敢打赌客户端不知道您的CA,因此,您需要在客户端安装证书/CA是的,我知道SSL使用证书…但我认为System.Net.Mail库可以处理它们…如何在客户端安装证书?如何在客户端安装证书?怎么做?我在用System.Net.Mail。有没有办法使用System.Net.Mail发布STARTTLS?建议如果SmtpClient.EnableSsl==trueI正在使用System.Net.Mail,则STARTTLS为默认值。有没有办法使用System.Net.Mail发布STARTTLS?建议如果SmtpClient.EnableSsl==True,则STARTTLS为默认值。由于它是一个事件处理程序,因此不需要在参数中传递任何内容。底层framewrok将为您实现这一点。如果你读过第一个代码块,你会看到连接它的调用。我已经添加了这个。但是,我还没有在存在问题的现场进行实际测试。我需要做的就是添加这些行?或者还有什么要做的吗?您可能希望将其更改为“记录到事件日志”。但是,是的,我在为客户远程诊断证书问题时开发了这个代码片段。当然,问题是根CA证书如@Andomar所述位于错误的存储中,但此脚本帮助说服了他们。您不需要在参数中传递任何内容,因为它是一个事件处理程序。底层framewrok将为您实现这一点。如果你读过第一个代码块,你会看到连接它的调用。我已经添加了这个。但是,我还没有在存在问题的现场进行实际测试。我需要做的就是添加这些行?或者还有什么要做的吗?您可能希望将其更改为“记录到事件日志”。但是,是的,我在为客户远程诊断证书问题时开发了这个代码片段。当然,问题是根CA证书如@Andomar所述位于错误的存储中,但是这个脚本帮助说服了他们。