简体   繁体   中英

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, S tring[] 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

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!

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".

const string username="apikey";
string pass = "xxxxxxxxxxxxxxxxxxxxx" // login sendgrid.com => create ApiKey
NetworkCredential vCredentials = new NetworkCredential("apikey", pass);

I have followed this guide Link: https://sendgrid.com/docs/API_Reference/SMTP_API/integrating_with_the_smtp_api.html

I did notice that a service was sending a lot of messages and using all the 25.000/month messages available in the azure free account plain. To solve the problem, I created a new account and fixed the service to send a lower number of messages.

For us - this non-descript error was due to reaching the sending limits for our sendgrid email api subscription. I would recommend checking your sendgrid account first before messing about with website settings.

我有同样的错误,但这是因为我在 NetworkCredential 中包含了一个域参数

I had the same issue, but it had to do with enabling 2FA in my sendgrid account before upgrading my SMTP and API V3 code to Key-based authentication.

If you enable 2FA before doing the other stuff, it breaks authentication.

In our case this was a network issue. We had to enable the company's firewall to allow web socket connections from our service to SendGrid/Twilio as the new C# client we had to switch to (SendGridClient) uses sockets. Previously we used the C# generic MailClient which used SMTP/HTTP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM