3
votes

I faced a problem when I get an exception using gmail smtp.

1) Allow less secure apps: ON

2) Tried ports 587, 465, 25 (same exceptions)

3) Tried to disable SSL (same exceptions)

4) telnet for smtp.gmail.com for ports 465, 587 working as well as direct sending from email address used for smtp

Ideas?

public async Task SendEmail(string subject, string message)
{
    MailAddress fromAddress = new MailAddress("[email protected]");
    MailAddress toAddress = new MailAddress("[email protected]");

    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("[email protected]", "password");

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

Exception using 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 using 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: After change client.Send(mail) to await client.SendMailAsync(mail) get new 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

Thanks for any help!

1
For testing I would recommend using IsBodyHtml = false in the MailMessage and also trying to enabled/disable SSL. Also SmtpClient has an awaitable SendMailAsync. As you are inside an async Task I would also recommend using and await this method. Regarding the timeout issue on port 587 it might be worth a shot at least checking your firewall(s). Although I doubt it if you can connect successfully via telnet on this port.Robin B
IsBodyHtml = false doesn't changed anything, but SendMailAsync produced few changes. For port 587 there is no exceptions at all, just running wheel without any response. For port 465 get new exception (pined to the topic)Mykyta Shvets
The email server (in this case GMAIL) determines which options and ports must be used for SMTP to work. Also the From email account and the credentials for sending the email must use the same email account. See following code project : codeproject.com/Tips/520998/…jdweng
SSL port 465, TLS - 587. Tried both. From and Credentials the same. Trying using doesn't produce any changes.Mykyta Shvets

1 Answers

1
votes

Finally get email working using Mail.dll (installed via NuGet). Example:

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