0

I am trying to send an email with C# code, copied from examples on MSDN (e.g. https://msdn.microsoft.com/en-us/library/14k9fb7t%28v=vs.110%29.aspx)

// from and password contain my credentials
// to contains a valid email address
public static void CodeExample()
{
    try
    {
        using (MailMessage mail = new MailMessage(from, to))
        {
            using (SmtpClient server = new SmtpClient("smtp.googlemail.com"))
            {
                mail.From = new MailAddress(from);
                mail.To.Add(new MailAddress(to));
                mail.Subject = "Test subject";
                mail.Body = "Test message";
                mail.IsBodyHtml = false;

                server.Port = 465;
                server.Credentials = new System.Net.NetworkCredential(from, password);
                server.UseDefaultCredentials = true;
                server.EnableSsl = true;
                server.ServicePoint.MaxIdleTime = 1;
                server.Timeout = 60000;

                Console.WriteLine("Sending to {0} by using SMTP host {1} port {2}.", to.ToString(), server.Host, server.Port);

                server.Send(mail);
                Console.WriteLine("mail Sent");
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
        Console.WriteLine("Inner Exception:");
        Console.WriteLine(ex.InnerException?.ToString());
    }
}

But I always get an exception:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.

The ‘from’ address details have been checked and seem OK. Sending from a Yahoo! account fails in the same way. I have tried lots of different combinations of SmtpClient properties. There are no messages in my firewall log.

Using Thunderbird, I can send from both the Googlemail and Yahoo! accounts without problems.

I would be grateful for any hints on how to get this to work.

Edit

I have seen this post SmtpException: Unable to read data from the transport connection: net_io_connectionclosed

Google mail fails on port 587 (both using and commenting-out UseDefaultCredentials = true and EnableSsl = true), reporting that I have an insecure app. I will try Yahoo! on port 587 later.

Community
  • 1
  • 1
Peter Bill
  • 508
  • 3
  • 12
  • 5
    `SmtpClient server` - I like that naming convention ;p – GolezTrol May 24 '16 at 21:00
  • 2
    Possible duplicate of [SmtpException: Unable to read data from the transport connection: net\_io\_connectionclosed](http://stackoverflow.com/questions/20228644/smtpexception-unable-to-read-data-from-the-transport-connection-net-io-connect) – Xiaoy312 May 24 '16 at 21:01
  • use smtp client as `new SmtpClient("smtp.gmail.com")` and assign server port the value `587` – Jamshaid K. May 24 '16 at 21:04

1 Answers1

0

Thanks for the help. Using port 587 was important, as shown at SmtpException: Unable to read data from the transport connection: net_io_connectionclosed

I still cannot get smtp.googlemail.com or smtp.gmail.com to work, but that is covered at SmtpClient with Gmail.

My program is now working with smtp.mail.yahoo.com.

Community
  • 1
  • 1
Peter Bill
  • 508
  • 3
  • 12