3 votes

exceptions smtp.gmail.com, permet d'envoyer des courriels

J'ai rencontré un problème lorsque j'ai reçu une exception en utilisant gmail smtp.

1) Autoriser les applications moins sécurisées : ON

2) Essai des ports 587, 465, 25 (mêmes exceptions)

3) J'ai essayé de désactiver SSL (mêmes exceptions).

4) telnet pour smtp.gmail.com pour les ports 465, 587 fonctionne ainsi que l'envoi direct depuis l'adresse email utilisée pour smtp

Des idées ?

public async Task SendEmail(string subject, string message)
{
    MailAddress fromAddress = new MailAddress("user_from@gmail.com");
    MailAddress toAddress = new MailAddress("user_to@gmail.com");

    MailMessage mail = new MailMessage(fromAddress.Address, toAddress.Address);
    mail.Subject = subject;
    mail.Body = message;

    SmtpClient client = new SmtpClient();
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.EnableSsl = true;
    client.Timeout = 20000;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential("user_from@gmail.com", "password");

    try
    {
        client.Send(mail);
    } 
    catch (Exception ex)
    {
        Console.Write(ex.Message);
    }
}

Exception utilisant le port 587 :

{System.Net.Mail.SmtpException: The operation has timed out.
    at System.Net.Mail.SmtpClient.Send(MailMessage message)
    at EmailsUtilities.<SendEmail>d__1.MoveNext() in client.Send(mail);

Exception en utilisant le port 465 :

{System.Net.Mail.SmtpException: Failure sending mail. ---> 
System.IO.IOException: Unable to read data from the transport connection: The 
connection was closed.
   at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 
offset, Int32 read, Boolean readLine)
    at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader 
caller, Boolean oneLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
   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)
   --- End of inner exception stack trace ---
  at System.Net.Mail.SmtpClient.Send(MailMessage message)
  at EmailsUtilities.<SendEmail>d__1.MoveNext() in client.Send(mail);

UPDATE : Après modification client.Send(mail) a await client.SendMailAsync(mail) obtenir une nouvelle exception :

{System.Net.Mail.SmtpException: Syntax error, command 
unrecognized. The server response was: 
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at 
System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult 
 result)
   at System.Net.Mail.SmtpTransport.EndGetConnection(IAsyncResult result)
   at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at 
System.Runtime.CompilerServices.TaskAwaiter
    .HandleNonSuccessAndDebuggerNotificati 
   on(Task task)
       at EmailsUtilities.<SendEmail>d__1.MoveNext() in client.SendMailAsync 
    System.Net.Mail.SmtpException

Merci de votre aide !

1voto

Mykyta Shvets Points 65

Enfin, le courrier électronique fonctionne à l'aide de Mail.dll (installé via NuGet). Exemple :

IMail email = Mail
    .Html(@"Html with an image: <img src=""cid:lena"" />")
    .AddVisual(@"c:\lena.jpeg").SetContentId("lena")
    .AddAttachment(@"c:\tmp.doc").SetFileName("document.doc")
    .To("to@example.com")
    .From("from@example.com")
    .Subject("Subject")
    .Create();

using(Smtp smtp = new Smtp())
{
    smtp.Connect("smtp.server.com");  // or ConnectSSL for SSL
    smtp.UseBestLogin("user", "password");
    smtp.SendMessage(email);                     
    smtp.Close();   
}

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X