2010-11-10 10 views
11

Mam na myśli to guide on C# SMTP mail.uwierzytelnianie lub odszyfrowywanie nie powiodło się podczas wysyłania poczty do GMail przy użyciu SSL

Oto mój kod:

MailMessage mail = new MailMessage(); 
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); 
mail.From = new MailAddress("[email protected]"); 
mail.To.Add("[email protected]"); 
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); 

Niestety, istnieje wyjątek odnośnie SSL i nie mogę go naprawić:

Unhandled Exception: System.Net.Mail.SmtpException: Message could not be sent. ---> System.IO.IOException: The authentication or decryption has failed. ---> 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
at Mono.Security.Protocol.Tls.SslClientStream.OnRemoteCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[] errors) [0x00000] in :0
at Mono.Security.Protocol.Tls.SslStreamBase.RaiseRemoteCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[] errors) [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
at 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 csharpdungeon.MainClass.Main() [0x00000] in :0

+1

ktoś już zaimplementował wysyłanie za pośrednictwem Gmaila (http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) –

Odpowiedz

14

Prawidłowym rozwiązaniem nie jest usunięcie weryfikacji certyfikatu SSL. GMail ma ważny certyfikat. Problem polega na tym, że Mono nie może znaleźć certyfikatu.

Jest to całkowicie błędne dla tak wielu powodów, ale głównie dlatego, że usuwa bardzo ważną poprawności certyfikatu:

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

Prawidłowe rozwiązanie jest, aby zainstalować certyfikat na komputerze:

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

Spowoduje to skuteczne pobranie certyfikatu dla Gmaila i udostępni go dla mono.

+0

Czy istnieje sposób na import certyfikatów? Piszę robota i muszę je zaimportować do sklepu monofonicznego, kiedy do nich dojdę. –

+0

Czy 'certmgr -ssl smtps: //smtp.gmail.com: 465' ślepo zezwoli na otrzymanie certyfikatu - i wystawi go na problem MITM? –

+0

To powinna być zaakceptowana odpowiedź! –

6

Nie masz odpowiednie władze certyfikat zainstalowany gdzieś gdzie Mono może je zobaczyć. Proszę spojrzeć na the Mono project security FAQ, aby dowiedzieć się, jak usunąć ten błąd.

-1

Czy musisz bezpiecznie wysyłać z SSL? Możesz usunąć to oświadczenie SmtpServer.EnableSsl = true;, które nie wymagałoby certyfikatu SSL.

+0

tak , GMail wymaga SSL AFAIK –

1

Czy próbowałeś zmianę portu, wiem domyślny port SMTP przez SSL jest 465

+1

Tak, ale zmienia się z "crashów" na "zawiesza się" –

23

Sprawdź, czy kod poniżej będzie działać dla ciebie; Przetestowałem go na moim koncie Gmail i wydaje się działać dobrze z moim mono 2.0 działa na ubuntu 10.04LTS

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("[email protected]"); 
      mail.To.Add("[email protected]"); 
      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); 
     } 
    } 
} 

rozwiązanie zaczerpnięte z here

nadziei, że to pomoże, chodzi

+0

Dobrze ^^ Dostałem pocztę, teraz spróbuję sformatować prawo –

+1

"Możesz być moim bohaterem!" – capdragon

+11

Kod faktycznie ignoruje błędy certyfikatu, nie jest dobre dla bezpieczeństwa. – Palani

0

Found innym rozwiązaniem z SmtpClient with Gmail

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

Jeśli uruchomiony mono serwer WWW, uruchom polecenie z samej nazwy użytkownika jako proces mono.

Powiązane problemy