无法使用webapi在Sendgrid上发送安全邮件

时间:2022-10-25 18:14:50

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

我无法使用web api在Sendgrid上发送“安全”邮件。(平台。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.

它在http url (http://sendgrid.com/api/mail.send)上运行良好,但在https (https://sendgrid.com/api/mail.send)上失败。

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

        //set the message recipients
        message.AddTo("to@sonedomain.com1");

        //set the sender
        message.From = new MailAddress("from@somedomain.com1");

        //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.

我得到一个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"

进一步深入代码,我收到这个错误:“400个错误请求,普通HTTP请求被发送到HTTPS端口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).

注意,它在http url i上运行良好。在上面的GetInstance函数中(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.

感谢您的帮助。

1 个解决方案

#1


4  

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

我在SendGrid工作。我已经做了一些测试,这是c#包装器的一个限制,特别是用于进行HTTP调用的CodeScales库。不幸的是,CodeScales根本不支持HTTPS。我在GitHub上打开了一个话题: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!

我将开始计划重构应用程序来使用RestSharp进行API调用。对不起,给你添麻烦了!

#1


4  

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

我在SendGrid工作。我已经做了一些测试,这是c#包装器的一个限制,特别是用于进行HTTP调用的CodeScales库。不幸的是,CodeScales根本不支持HTTPS。我在GitHub上打开了一个话题: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!

我将开始计划重构应用程序来使用RestSharp进行API调用。对不起,给你添麻烦了!