Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SendGrid Unable to read data from the transport connection: net_io_connectionclosed

I am getting an exception thrown sending an email via SendGrid since recently upgrading a project to .net 4.5.2

Failure sending mail. System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed. 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.CheckCommand.Send(SmtpConnection conn, String& response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at System.Net.Mail.SmtpClient.Send(MailMessage message) at SendGridMail.Transport.SMTP.SmtpWrapper.Send(MailMessage mime) at SendGridMail.Transport.SMTP.Deliver(ISendGrid message) at ReACT.Classes.Business.Helpers.Email.Send(String[] to, String[] toNames, Boolean ccToSender, String[] ccTo, String[] ccToNames, String subject, String body, Boolean isHtml, String SMTPServer, String EmailUserName, String EmailPassword, String EmailPort, String SystemEmailAddress, String SystemEmailName, String& FriendlyException, String& TechnicalException)

The code used to send the email via SMTP using the SendGrid service is as follows -

            SendGridMail.SendGrid vEmailMessage = SendGridMail.SendGrid.GetInstance(vMailMessage.From, vMailMessage.To.ToArray(), vMailMessage.CC.ToArray(), new MailAddress[0], vMailMessage.Subject, vMailMessage.Body, vMailMessage.Body);

            NetworkCredential vCredentials = new NetworkCredential(this.ApplicationSettings.EmailUserName, this.ApplicationSettings.EmailPassword);
            var vTransport = SMTP.GetInstance(vCredentials);

            //Send email message
            vTransport.Deliver(vEmailMessage);

The credentials are correct and confirmed as working correctly. This problem has only started since the .net framework upgrade and unfortunately, we cannot downgrade back to target .net 4

like image 900
AVFC_Bubble88 Avatar asked Nov 19 '15 16:11

AVFC_Bubble88


2 Answers

For those experiencing the same exception when sending emails via SendGrid, it turned out that a new piece of functionality was passing an incorrect password to the SendGrid API resulting in a AuthenticationFailedException: 535 Authentication failed: Bad username / password.

I discovered this after downloading Wireshark, finding & inspecting the SendGrid packets to find the data sent to the API was incorrect under a certain condition. The SendGrid API then returned a rather helpful AuthenticationFailedException exception however when this exception was caught in a try catch block in code, the actual exception was masked and came out as the aforementioned net_io_connectionclosed IOException

I fixed the bug in our new application and the issue went away. If only the actual exception being thrown by the SendGrid API was the one caught in the try catch block!

like image 70
AVFC_Bubble88 Avatar answered Sep 19 '22 09:09

AVFC_Bubble88


I had the same problem. I had the wrong username. I thought this was the ApiKey that I created for this, I even tried the email with and without domain. The solution was that the SMTP username is "apikey", as in that exact string, not your apikey, not the internal identifier nor the 'friendly' name you gave it. Just the exact 6 letters of "apikey".

like image 34
Leo Muller Avatar answered Sep 22 '22 09:09

Leo Muller