1

In May 2022, Google no longer supports the "Allow less secure apps" feature. So , I enabled two step verification in gmail using my phone and generated an App Password (16 digit random string) and used it in my C# code, but getting this excepton

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

The emails are read but unable to send emails, Please assist.

    //Send email to client...
                SmtpClient objSmtpClient = new SmtpClient();
                objSmtpClient.UseDefaultCredentials = false;
                objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                objSmtpClient.EnableSsl = true;
                objSmtpClient.Port = 587;
    
                Email objSendMail = new Email();
    
                objSmtpClient.Host = "smtp.gmail.com";
                objSmtpClient.Credentials = new NetworkCredential("example@gmail.com","MyAppPassword");
                objSendMail.From = "example@gmail.com";
                objSendMail.To = customerEmail;
                
    
                objSendMail.Subject = "Test subject";
    
                
                objSendMail.Body = "Test Body";
    
                try
                {
                    var isSendEmail = objSendMail.SendEmail(objSmtpClient);
                }
                catch (Exception ex)
                {
                    AppLogger.LogError(ex.Message + Environment.NewLine + ex.ToString());
                    return false;
                }
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
DfrDkn
  • 1,270
  • 2
  • 16
  • 23
  • SMTP is similar to HTTP and will fail if special character are found. See : https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references?force_isolation=true. Binary data has to be put in MIME format. See : https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references?force_isolation=true – jdweng Jun 14 '22 at 12:06
  • You should consider using Xoauth instead of an apps password https://stackoverflow.com/q/72614113/1841839 – Linda Lawton - DaImTo Jun 14 '22 at 12:25
  • Try using port 465 – Charlieface Jun 14 '22 at 13:07

2 Answers2

0

After configuring an apps password this works.

 using (var client = new SmtpClient())
    {
        client.Connect("smtp.gmail.com", 465, true);
        client.Authenticate("xxxx@gmail.com", "AppsPassword");
        client.Send(message.GetMessage());
        client.Disconnect(true);
    }

Another option would be xoauth2

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
0

Definitelly, it seems to be related to your Google Email Account, specifically. I'm facing the same issue in one of my Google accounts, even after I setup Two Factor Authenticator and App Password, like Google instructions (once "access to less secure apps" was discontinued, since 2022-may-30). When I try to send an email, I receive the following error message:

Unable to read data from the transport connection: The connection was closed.

So, after a couple of tries without success, I decided to try with another Google Account, making the same setup (Two Factor Authenticator an App Password creation), and the same source code I was using did the work.

I have contact Google in the following address in order to try to solve this issue in my specific account in the following contact url:

https://support.google.com/accounts/contact/less_secure_apps

Having feedback I will post it here.

Carretoni
  • 1
  • 1