0

I can't understand why this code is not working.

I have this error:

  • System.IO.IOException: Connection closed at System.Net.Mail.SmtpClient.Read ()

  • System.IO.IOException: Connection closed at System.Net.Mail.SmtpClient.SendCore

  • System.IO.IOException: Connection closed at System.Net.Mail.SmtpClient.SendInternal

  • System.IO.IOException: Connection closed at System.Net.Mail.SmtpClient.Send

        static void Main(string[] args)
        {
            var smtp = new SmtpClient
            {
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                EnableSsl = true,
                Host = "smtp.gmail.com",
                //465 SSL se uso 25 solo ad utenti google mando
                Port = 465, 
                Credentials = new NetworkCredential("id", "password"),
            };
    
            Console.WriteLine("Mail From: ");
            var fromAddress = new MailAddress(Console.ReadLine());
    
            Console.WriteLine("Mail To: ");
            var toAddress = new MailAddress(Console.ReadLine());
    
            Console.WriteLine("Subject: ");
            string subject = Console.ReadLine();
    
            Console.WriteLine("Body: ");
            string body = Console.ReadLine();
    
            using (var message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = body
            })
    
            try
            {
                smtp.Send(message);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Unable to send message due to the following reason: " + ex.ToString());
            }
        }
    

how can I solve these problems?

th3g3ntl3man
  • 1,926
  • 5
  • 29
  • 50
  • 1
    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) – Niranjan Singh Sep 27 '16 at 12:29

2 Answers2

1

Try port 587 instead of 465. Port 465 is technically deprecated.

  • You can find a similar problem here: http://stackoverflow.com/questions/20228644/smtpexception-unable-to-read-data-from-the-transport-connection-net-io-connect – iDesireJustice Sep 27 '16 at 12:21
  • Even if I set the port on 587 I have the same errors. In addition, Gmail tells me that someone attempted to access my account. – th3g3ntl3man Sep 27 '16 at 12:26
  • @JeremyShiklov if you are only going to copy and paste an answer then you should simply flag the question as a duplicate. – user1666620 Sep 27 '16 at 12:55
  • I'm new in Stackoverflow, so I don't really know how to do it. And if you'd read his comment, he said it doesn't help him, therefore it's not the same problem and not the same question. I just gave him a reference to a question which might help him because he should know his code better than us. – iDesireJustice Sep 27 '16 at 13:32
0

from the OP's comment:

Even if I set the port on 587 I have the same errors. In addition, Gmail tells me that someone attempted to access my account

If that's the case when you are using port 587 then you should allow less secure applications in your gmail settings. If you are using 2 factor authentication then you also need to add an application password and use that instead.

user1666620
  • 4,800
  • 18
  • 27