简体   繁体   English

使用SSL向GMail发送邮件时,身份验证或解密失败

[英]authentication or decryption has failed when sending mail to GMail using SSL

I'm referring to this guide on C# SMTP mail . 我指的是关于C#SMTP邮件的指南

Here is my code: 这是我的代码:

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("dave.stockinger@gmail.com");
mail.To.Add("dave.stockinger@gmail.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("MyUserNameInGmail", "MyGmailPassWord");
SmtpServer.EnableSsl = true;

SmtpServer.Send(mail);

Unfortunately, there's an exception regarding SSL and I can't fix it: 不幸的是,有一个关于SSL的例外,我无法修复它:

Unhandled Exception: System.Net.Mail.SmtpException: Message could not be sent. 未处理的异常:System.Net.Mail.SmtpException:无法发送消息。 ---> System.IO.IOException: The authentication or decryption has failed. ---> System.IO.IOException:身份验证或解密失败。 ---> System.InvalidOperationException: SSL authentication error: RemoteCertificateNotAvailable, RemoteCertificateChainErrors at System.Net.Mail.SmtpClient.m__4 (System.Object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, SslPolicyErrors sslPolicyErrors) [0x00000] in :0 at System.Net.Security.SslStream+c__AnonStorey7.<>m__A (System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Int32[] certErrors) [0x00000] in :0 ---> System.InvalidOperationException:SSL身份验证错误:RemoteCertificateNotAvailable,System.Net.Mail.SmtpClient.m__4上的RemoteCertificateChainErrors(System.Object sender,System.Security.Cryptography.X509Certificates.X509Certificate证书,System.Security.Cryptography.X509Certificates。 X509链表,SslPolicyErrors sslPolicyErrors)[0x00000] in:0表示System.Net.Security.SslStream + c__AnonStorey7。<> m__A(System.Security.Cryptography.X509Certificates.X509Certificate cert,System.Int32 [] certErrors)[0x00000]: 0
at Mono.Security.Protocol.Tls.SslClientStream.OnRemoteCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[] errors) [0x00000] in :0 在Mono.Security.Protocol.Tls.SslClientStream.OnRemoteCertificateValidation(System.Security.Cryptography.X509Certificates.X509Certificate证书,System.Int32 []错误)[0x00000] in:0
at Mono.Security.Protocol.Tls.SslStreamBase.RaiseRemoteCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[] errors) [0x00000] in :0 在Mono.Security.Protocol.Tls.SslStreamBase.RaiseRemoteCertificateValidation(System.Security.Cryptography.X509Certificates.X509Certificate证书,System.Int32 []错误)[0x00000] in:0
at Mono.Security.Protocol.Tls.SslClientStream.RaiseServerCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[] certificateErrors) [0x00000] in :0 at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates) [0x00000] in :0 at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1 () [0x00000] in :0 在Mono.Security.Protocol.Tls.SslClientStream.RaiseServerCertificateValidation(System.Security.Cryptography.X509Certificates.X509Certificate证书,System.Int32 [] certificateErrors)[0x00000] in:0 at Mono.Security.Protocol.Tls.Handshake.Client。 TlsServerCertificate.validateCertificates(Mono.Security.X509.X509CertificateCollection证书)[0x00000] in:0 at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1()[0x00000] in:0
at Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process () [0x00000] in :0 在Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process()[0x00000] in:0
at (wrapper remoting-invoke-with-check) Mono.Security.Protocol.Tls.Handshake.HandshakeMessage:Process () at Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage (Mono.Security.Protocol.Tls.TlsStream handMsg) [0x00000] in :0 at Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult) [0x00000] in :0 --- End of inner exception stack trace --- at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) [0x00000] in :0 --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMessage message) [0x00000] in :0 at(wrapper remoting-invoke-with-check)Mono.Security.Protocol.Tls.Handshake.HandshakeMessage:Process()at Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage(Mono.Security.Protocol.Tls.TlsStream handMsg) [0x00000] in:0 at Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback(IAsyncResult asyncResult)[0x00000] in:0 ---内部异常堆栈跟踪结束---在Mono.Security.Protocol.Tls.SslStreamBase .AsyncHandshakeCallback(IAsyncResult asyncResult)[0x00000] in:0 ---内部异常堆栈跟踪结束---在System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMessage消息)[0x00000] in:0
at csharpdungeon.MainClass.Main () [0x00000] in :0 在csharpdungeon.MainClass.Main()[0x00000] in:0

Check if code below would work for you; 检查以下代码是否适合您; I've tested it on my gmail account and it seem to work fine with my mono 2.0 running on ubuntu 10.04LTS 我已经在我的Gmail帐户上测试了它,它似乎与我在ubuntu 10.04LTS上运行的mono 2.0一样正常

using System;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace mono_gmail
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            MailMessage mail = new MailMessage();

            mail.From = new MailAddress("my.name@gmail.com");
            mail.To.Add("my.name@hotmail.com");
            mail.Subject = "Test Mail";
            mail.Body = "This is for testing SMTP mail from GMAIL";

            SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
            smtpServer.Port = 587;
            smtpServer.Credentials = new System.Net.NetworkCredential("my.name", "my.password");
            smtpServer.EnableSsl = true;
            ServicePointManager.ServerCertificateValidationCallback = 
                delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
                    { return true; };
            smtpServer.Send(mail);
        }
    }
}

solution is taken from here 解决方案取自这里

hope this helps, regards 希望这有帮助,问候

The correct solution is not to remove SSL Certificate Validation. 正确的解决方案不是删除SSL证书验证。 GMail has a valid certificate. GMail拥有有效证书。 The problem is that Mono can't seem to find the certificate. 问题是Mono似乎无法找到证书。

This is completely wrong for so many reasons, but mainly because it removes a very important certificate validation: 出于这么多原因,这是完全错误的,但主要是因为它删除了一个非常重要的证书验证:

ServicePointManager.ServerCertificateValidationCallback = 
            delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
                { return true; };

The correct solution is to install the certificate in your machine: 正确的解决方案是在您的机器中安装证书:

mozroots --import --ask-remove --machine
certmgr -ssl smtps://smtp.gmail.com:465

This will effectively download the certificate for gmail and it will make it available for mono to use. 这将有效地下载gmail证书,并使其可供单声道使用。

You do not have the appropriate certificate authorities installed somewhere where Mono can see them. 您没有在Mono可以看到它们的地方安装相应的证书颁发机构。 Please have a look at the Mono project security FAQ for the steps to remedy this error. 有关解决此错误的步骤,请查看Mono项目安全性常见问题解答

If you're using SSL rather than TLS, you need port 465 rather than 587. 如果您使用的是SSL而不是TLS,则需要端口465而不是587。

See http://mail.google.com/support/bin/answer.py?hl=en&answer=13287 . 请参阅http://mail.google.com/support/bin/answer.py?hl=zh-CN&answer=13287

您是否尝试更改端口,我知道SSL的默认SMTP端口是465

Found another solution from SmtpClient with Gmail 通过Gmail找到了SmtpClient的另一个解决方案

certmgr -ssl smtps://smtp.gmail.com:465

If you are running mono web server, run command with same username as mono process. 如果您正在运行单声道Web服务器,请使用与mono进程相同的用户名运行命令。

Do you need to send securely with SSL? 您需要使用SSL安全发送吗? You could remove this statement SmtpServer.EnableSsl = true; 您可以删除此语句SmtpServer.EnableSsl = true; which would not require the SSL cert. 这不需要SSL证书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM