SmtpClient avec Gmail

Je suis le développement d'un client de messagerie pour un projet d'école. J'ai réussi à envoyer des e-mails à l'aide de la SmtpClient en C#. Cela fonctionne parfaitement avec n'importe quel serveur, mais il ne fonctionne pas avec Gmail. Je crois que c'est à cause de Google à l'aide de TLS. J'ai essayé de réglage EnableSsl à true sur le SmtpClient mais ce n'est pas faire une différence.

C'est le code que j'utilise créer le SmtpClient et envoyer un e-mail.

this.client = new SmtpClient("smtp.gmail.com", 587);
this.client.EnableSsl = true;
this.client.UseDefaultCredentials = false;
this.client.Credentials = new NetworkCredential("username", "password");

try
{
    //Create instance of message
    MailMessage message = new MailMessage();

    //Add receiver
    message.To.Add("[email protected]");

    //Set sender
    //In this case the same as the username
    message.From = new MailAddress("[email protected]");

    //Set subject
    message.Subject = "Test";

    //Set body of message
    message.Body = "En test besked";

    //Send the message
    this.client.Send(message);

    //Clean up
    message = null;
}
catch (Exception e)
{
    Console.WriteLine("Could not send e-mail. Exception caught: " + e);
}

C'est l'erreur que j'obtiens quand j'essaie d'envoyer un e-mail.

Could not send e-mail. Exception caught: 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.<callback>m__4 (System.Object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, SslPolicyErrors sslPolicyErrors) [0x00000] in <filename unknown>:0 
at System.Net.Security.SslStream+<BeginAuthenticateAsClient>c__AnonStorey7.<>m__A (System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Int32[] certErrors) [0x00000] in <filename unknown>:0 
at Mono.Security.Protocol.Tls.SslClientStream.OnRemoteCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[] errors) [0x00000] in <filename unknown>:0 
at Mono.Security.Protocol.Tls.SslStreamBase.RaiseRemoteCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[] errors) [0x00000] in <filename unknown>:0 
at Mono.Security.Protocol.Tls.SslClientStream.RaiseServerCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[] certificateErrors) [0x00000] in <filename unknown>:0 
at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates) [0x00000] in <filename unknown>:0 
at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1 () [0x00000] in <filename unknown>:0 
at Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process () [0x00000] in <filename unknown>: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 <filename unknown>:0 
at Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 
--- End of inner exception stack trace ---
at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMessage message) [0x00000] in <filename unknown>:0 
at P2Mailclient.SMTPClient.send (P2Mailclient.Email email) [0x00089] in /path/to/my/project/SMTPClient.cs:57 

Quelqu'un a une idée de pourquoi j'ai peut-être cette erreur?

  • essayez de définir client.UseDefaultCredentials = false; avant de définir les informations d'identification.
  • Qui n'a pas fait une différence.
  • Ressemble au problème de certificat - voir ma réponse.
  • J'ai édité ma réponse et suis en train d'écrire commentaire suivant de vous avoir informé. Veuillez voir le fichier de réponse.
  • Je ne suis pas à mon ordinateur pour l'instant, mais je n'hésiterais pas à y jeter un oeil dès que possible. Je vous remercie pour votre aide.
InformationsquelleAutor simonbs | 2012-03-21