Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SendGrid SMTP integration issue

I am trying to integrate SendGrid in ASP.NET MVC application using SmtpClient and MailMessage methods on Azure.

Code:

MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.Subject = "Subject";
message.To.Add("[email protected]");
message.Body = "Body";
message.From = new MailAddress("[email protected]", "From Name");

SmtpClient client = new SmtpClient("smtp.sendgrid.net");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("??", "SG.******");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Port = 587; // I have tried with 25 and 2525
client.Timeout = 99999;
client.EnableSsl = false;

client.Send(message);

I end up with this issue both from C# console application and ASP.NET MVC application on Azure VM:

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

As per the documentation avlb on SendGrid site: https://sendgrid.com/docs/Classroom/Basics/Email_Infrastructure/recommended_smtp_settings.html

The UserName and Password has to : Use the string “apikey” for the SMTP username and use your API key for the password.

I have tried -

  1. SendGrid UserName with which I login to their portal

  2. The API Key from this page https://app.sendgrid.com/settings/api_keys

  3. The password which starts with SG* which is as per their support team.

Any suggestions what am I missing or what which UserName/APIKey should I be using?

Tx!

like image 240
AaBa Avatar asked Sep 29 '17 01:09

AaBa


People also ask

How do I integrate with SendGrid's SMTP API?

To integrate with SendGrid's SMTP API: Create an API Key with at least "Mail" permissions. Set the server host in your email client or application to smtp.sendgrid.net. This setting is sometimes referred to as the external SMTP server or the SMTP relay.

What SMTP ports does SendGrid support?

Sendgrid allows you to connect on ports 25 or 587. As of October 2020, you must authenticate into the Sendgrid SMTP relay with the username “apikey” (this is the same for everyone) and then your actual API key that you create from inside the SendGrid interface.

Why can’t I send emails with SendGrid?

For this, you will likely need to authenticate your domain so that email services know that SendGrid is fully authorised to send emails on your behalf. Your API key isn’t working correctly. Try regenerating a new API key and make sure it has full access.

How do I send emails with Twilio SendGrid?

With Twilio SendGrid, you can send emails without worrying about scalability. You can use the SendGrid APIs to send emails, but if you're already using SMTP in your code, you can also use the SMTP protocol with SendGrid. In this post, you will learn how to send emails using SMTP, SendGrid, and a .NET 6 console application.


1 Answers

The link says : Use the string “apikey” for the SMTP username and use your API key for the password.

https://sendgrid.com/docs/Classroom/Basics/Email_Infrastructure/recommended_smtp_settings.html

The username was suppose to be "apikey" and not the actual "api key or api name"

client.Credentials = new NetworkCredential("apikey", 
                                           "<Your API Key which starts with SG.*>");

Adding "apikey" (face palm) fixed my problem.

like image 88
AaBa Avatar answered Sep 30 '22 06:09

AaBa