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 -
SendGrid UserName with which I login to their portal
The API Key from this page https://app.sendgrid.com/settings/api_keys
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!
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With