Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to send secure emails on Sendgrid using webapi

I am unable to send "secure" emails on Sendgrid using web api. (Platform - .Net/C#, SendgridMail)

It works fine with http url (http://sendgrid.com/api/mail.send) but with https (https://sendgrid.com/api/mail.send) it fails.

   private void SendMail()
    {
        var message = SendGrid.GenerateInstance();

        //set the message recipients
        message.AddTo("[email protected]");

        //set the sender
        message.From = new MailAddress("[email protected]");

        //set the message body
        message.Html = "<html><p>Hello</p><p>World</p></html>";

        //set the message subject
        message.Subject = "Hello World HTML Test";

        //create an instance of the Web transport mechanism
        var transportInstance = SendGridMail.Transport.REST.GetInstance(new NetworkCredential("myusername", "mypassword"),"https://sendgrid.com/api/mail.send");

        //send the mail
        transportInstance.Deliver(message);
    }

I get an ArgumentException: Unknown element: html.

Further drilling down the code, I receive this error: "400 Bad Request, The plain HTTP request was sent to HTTPS port, nginx/0.7.65"

Note
It works fine with http url i.e : (http://sendgrid.com/api/mail.send) in GetInstance function above instead of (https://sendgrid.com/api/mail.send).

Sending email request through browser works fine: (https://sendgrid.com/api/mail.send.json?to=to%40somedomain.com1&from=from%40somdomain.com1&subject=Test%20SG%20API&text=sometext&html=%3Cb%3E%20test%20SG%20api%20body&api_user=sendgridusername&api_key=sendgridpassword)

An help on this is appreciated.

like image 452
user1891155 Avatar asked Oct 05 '22 17:10

user1891155


1 Answers

I work for SendGrid. I've done some testing and this is a limitation of the C# wrapper, specifically the CodeScales library that is being used to make the HTTP calls. CodeScales unfortunately doesn't support HTTPS at all. I have opened an issue on GitHub for this: https://github.com/sendgrid/sendgrid-csharp/issues/21

I will start planning to refactor the app to use RestSharp for making the API calls. Sorry for the trouble!

like image 193
bwest Avatar answered Oct 10 '22 03:10

bwest