11

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("To@MyDomain.com");
message.Body = "Body";
message.From = new MailAddress("From@MyDomain.com", "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!

AaBa
  • 451
  • 1
  • 6
  • 21
  • 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 Jan 16 '20 at 15:56

2 Answers2

28

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

https://docs.sendgrid.com/for-developers/sending-email/v2-csharp-code-example#using-nets-built-in-smtp-library

The username was supposed 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" (facepalm) fixed my problem.

Chintan
  • 454
  • 6
  • 15
AaBa
  • 451
  • 1
  • 6
  • 21
  • 1
    Wow, even their official documentation is wrong: https://sendgrid.com/docs/for-developers/sending-email/v2-csharp-code-example/ they say to use the username as username which is not working and gives weird IOException. Good that I've found this solution, the username I confirm in August 2020 should be "apikey" (and use the api key for the password). – firepol Aug 23 '20 at 13:46
0

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;
Brando Zhang
  • 22,586
  • 6
  • 37
  • 65
  • 1
    There are no such outbound rules, so it is not a port issue. – AaBa Sep 29 '17 at 05:01
  • I have write a test demo on my VM, It works well, I suggest you could try my codes and test again. – Brando Zhang Sep 29 '17 at 05:11
  • Hi, thanks for going an extra mile, appreciate it! I didn't had an luxury to modify the code, as I have configure the table with SMTP address, userID, password and port. So SMTP was the only way. The key was to use "apikey" as username and it started working. – AaBa Sep 29 '17 at 05:13