版本 .NetCore 3.1
系统 CentOs 7
发送邮件,本地测试OK , 发布后报错
System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
使用自带的Smtp和MailKit.Smtp 均无效
提示应该是证书问题.
代码如下 :
1 MailMessage mailMsg = new MailMessage();//实例化对象 2 mailMsg.From = new MailAddress("xxxx", "xxxx");//源邮件地址和发件人 3 mailMsg.To.Add(new MailAddress("xxx@126.com"));//收件人地址 4 mailMsg.Subject = "邮件发送测试";//发送邮件的标题 5 StringBuilder sb = new StringBuilder(); 6 sb.Append("测试测试测试测试"); 7 sb.Append("嘿嘿"); 8 mailMsg.Body = sb.ToString();//发送邮件的内容 9 //指定smtp服务地址(根据发件人邮箱指定对应SMTP服务器地址) 10 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();//格式:smtp.126.com smtp.164.com 11 client.Host = "smtp.xxx.com"; 12 //要用587端口 13 client.Port = 587;//端口 14 //加密 15 client.EnableSsl = true; 16 //通过用户名和密码验证发件人身份 17 client.Credentials = new NetworkCredential("xxx@xxx.com", "xxxxxxx"); // 18 //发送邮件 19 try 20 { 21 client.Send(mailMsg); 22 return "success"; 23 } 24 catch (Exception ex) 25 { 26 return ex.ToString(); 27 }
返回具体信息
System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure. at System.Net.Security.SslStream.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, ExceptionDispatchInfo exception) at System.Net.Security.SslStream.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslStream.ProcessAuthentication(LazyAsyncResult lazyResult, CancellationToken cancellationToken) at System.Net.Security.SslStream.AuthenticateAsClient(SslClientAuthenticationOptions sslClientAuthenticationOptions) at System.Net.Security.SslStream.AuthenticateAsClient(String targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation) at System.Net.TlsStream.AuthenticateAsClient() 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)
这会是什么问题呢? 求赐教
试试 MailKit 下面的配置
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
参考 http://www.mimekit.net/docs/html/Frequently-Asked-Questions.htm
试过了,完美解决 , 感谢