17

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

AVFC_Bubble88
  • 473
  • 1
  • 3
  • 15

8 Answers8

16

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

Leo Muller
  • 1,421
  • 1
  • 12
  • 20
  • In my case, I was using the old UserName/Password paradigm, which suddenly appears to no longer be supported by SendGrid. Changing to "apikey" as the UserName and the key itself as the password fixed the issue. – Joe Newton Feb 18 '21 at 19:53
  • Wow! Of all the stupid things! the user name is "apikey"!!!!!!!!!! Thank you so much. – Frinavale Jun 30 '23 at 18:55
13

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!

AVFC_Bubble88
  • 473
  • 1
  • 3
  • 15
  • how did you fix the issue? – zillabunny Sep 09 '16 at 17:22
  • I found that the problem was caused by an incorrect password - the password stored was correct but the logic to retrieve it from storage had an issue in it and once it was resolved, and the correct password being passed, all worked – AVFC_Bubble88 Sep 14 '16 at 11:30
  • interesting our issue was with sendgrid themselves, our credentials were correct and we could use them to get into sendgrid but we created quick little C# smtp mailer and it didn't work, we created a new sendgrid account and it worked for us. – zillabunny Sep 15 '16 at 13:55
  • 1
    We had a bug that used all of our email quota up! I found this looking at the https://app.sendgrid.com portal. – Michael Blake Jan 17 '17 at 12:35
  • I had this issue because I slavishly followed the Sendgrid c# tutorial which used `Environment.GetEnvironmentVariable` to retrieve the api key where in fact I should have been using `ConfigurationManager.AppSettings` in order to retrieve it from the web.config file I was using. – keithl8041 Aug 20 '18 at 20:09
5
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

MinhDev
  • 51
  • 1
  • 4
2

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.

Jarrette
  • 1,085
  • 2
  • 16
  • 40
  • 1
    Yeah, I set up the SendGrid account, set up the API key, and didn't do anything with it until the next day. The next day, when I signed on, it had me set up the 2FA. It didn't occur to me that that could have broken my API key. But I just generated a new one and it worked. – John M Gant Jul 12 '22 at 19:02
2

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.

user2575424
  • 101
  • 5
1

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.

1

I had the same error but it was because I had included a domain parameter in the NetworkCredential

Michael Armitage
  • 1,502
  • 19
  • 17
  • That was my issue. Thank you so much for that. I have a "generic" method that retrieves credentials for various services and it was automatically including the domain parameter in the return `NetworkCredential` value. By adding an **"`IncludeDomain`"** parameter for the method and passing a **`False`** value for SendGrid, I was able to get it working again. – G_Hosa_Phat Aug 14 '23 at 15:19
0

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.

Vedran Mandić
  • 1,084
  • 11
  • 21