10
votes

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!

2
Side question, can you confirm that the following settings are secure? With enablessl = false, does this mean your emails are not encrypted when being sent to sendgrid servers? - TWilly

2 Answers

24
votes

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.

0
votes

According to your description, I guess you may not add the VM outbound role for port 587.

I suggest you could follow below steps to add the role.

Find the network in azure vm portal.

enter image description here

Then find add outbound rule.

enter image description here

3.Add 587 port to outbound role.

enter image description here


Update:

I suggest you could try to use sendgrid SDK to do this.

I have create a test console application, it works well in my side.

Library: SendGrid

Codes as below:

        var client = new SendGridClient("sendgridkey");
        var from = new EmailAddress("send email address", "Example User");
        var subject = "Sending with SendGrid is Fun";
        var to = new EmailAddress("To email address", "Example User");
        var plainTextContent = $"Name : aaaa";
        var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
        var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
         client.SendEmailAsync(msg).Wait();

        int i = 0;