Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclose

1k views Asked by At

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("[email protected]","MyAppPassword");
                objSendMail.From = "[email protected]";
                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;
                }
2

There are 2 answers

0
Linda Lawton - DaImTo On

After configuring an apps password this works.

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

Another option would be xoauth2

0
Carretoni On

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.