sendgrid-csharp icon indicating copy to clipboard operation
sendgrid-csharp copied to clipboard

response.StatusCode == UnsupportedMediaType

Open omric-axonize opened this issue 10 years ago • 3 comments

Issue Summary

vs2015, .net 4.6.1, nuget package SendGrid 8.0.4 response StatusCode is always UnsupportedMediaType tried it with content type "text/plain" and "text/html"

var sg = new SendGridAPIClient(ConfigurationManager.AppSettings["sendGridKey"]);

var to = new Email(emailNotification.Address);
var from = new Email(ConfigurationManager.AppSettings["emailFrom"], ConfigurationManager.AppSettings["emailDisplayName"]);

var subject = emailNotification.Subject;
var type = "text/html";
var content = new Content(type, emailNotification.Message);

var mail = new Mail(from, subject, to, content);

var response = await sg.client.mail.send.post(requsetBody: mail.Get());

Steps to Reproduce

  1. copy this code to an async method

Technical details:

  • sendgrid-csharp Version: 8.0.4
  • .NET Version: 4.6.1

omric-axonize avatar Sep 20 '16 08:09 omric-axonize

@omric-axonize,

I'm guessing this is a compatibility issue with .net 4.6.1, as this library officially supports 4.5.2. We are working to provide wider support (see here), but in the mean time. I suggest you download the source and build with 4.6.1 as the target.

Please let us know how that goes. Thanks!

thinkingserious avatar Sep 22 '16 00:09 thinkingserious

Hi,

I'm experiencing the same issue, but im using .NET 4.5.2. Do you have any suggestions?

bkappel avatar Sep 23 '16 12:09 bkappel

@thinkingserious Thanks but I've switched to send JSON and everything works like a charm. This is my code now:

var sg = new SendGridAPIClient(apiKey);

var data = new JObject
{
    new JProperty("personalizations", new JArray
    {
        new JObject
        {
            new JProperty("to",
                new JArray
                {
                    new JObject
                    {
                        new JProperty("email", emailNotification.Address)
                    }
                }),
            new JProperty("subject", emailNotification.Subject)
        }
    }),
    new JProperty("from", new JObject { 
        new JProperty("email", emailFrom)}),
    new JProperty("content", new JArray
    {
        new JObject
        {
            new JProperty("type", "text/plain"),
            new JProperty("value", emailNotification.Message)
        }
    })
};
Response response = await sg.client.mail.send.post(requestBody: data.ToString());
return response.StatusCode == HttpStatusCode.Accepted;

omric-axonize avatar Sep 25 '16 06:09 omric-axonize