Sendgrid-csharp: Adding HTML Content Before Plain Text Content Results In Bad Request Response

Created on 19 Oct 2016  路  6Comments  路  Source: sendgrid/sendgrid-csharp

Issue Summary

I realize that a new branch is in progress to bring back static typing - raising in case this is an issue there as well.

 // Get Bad Request in response.
var client = new SendGridAPIClient(_appSettingsReader.Read<string>("SendGridApiKey"));

var emailBodyWrapper = JsonConvert.DeserializeAnonymousType(message.Body,
    new { Html = string.Empty, Text = string.Empty });

    var mail =
        new Mail(
            new Email(_appSettingsReader.Read<string>("SendGridFromAddress"),
                _appSettingsReader.Read<string>("SendGridFromName")), message.Subject,
            new Email(message.Destination), new Content("text/html", emailBodyWrapper.Html));
mail.AddContent(new Content("text/plain", emailBodyWrapper.Text));

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

// Works fine and e-mail delivered.
var client = new SendGridAPIClient(_appSettingsReader.Read<string>("SendGridApiKey"));
var emailBodyWrapper = JsonConvert.DeserializeAnonymousType(message.Body,
    new { Html = string.Empty, Text = string.Empty });

var mail =
    new Mail(
        new Email(_appSettingsReader.Read<string>("SendGridFromAddress"),
            _appSettingsReader.Read<string>("SendGridFromName")), message.Subject,
        new Email(message.Destination), new Content("text/plain", emailBodyWrapper.Text));
mail.AddContent(new Content("text/html", emailBodyWrapper.Html));

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

image

Steps to Reproduce

  1. Create a new Mail object with HTML content specified in constructor.
  2. Add text/plain content using AddContent.
  3. Attempt to send the email.

Any other information you want to share that is relevant to the issue being reported. Especially, why do you consider this to be a bug? What do you expect to happen instead?

I would expect to be able to add text/plain and text/html content in any order.

Technical details:

  • sendgrid-csharp Version: 8.0.4 Nuget package
  • .NET Version: 4.6.1 targeted, 4.6.2 installed.
help wanted bug

All 6 comments

Thanks for the heads up @brendonwm!

I'd like to ask that the error message also be changed to indicate that there can't be multiple text/plain and text/html Content items. The current error implies you can have multiple Contents of each type, but they must be grouped separately. In reality, only >>one<< instance of each type is allowed, and the "plain" one must come before the "html" one.

I can't tell you how many hours I've wasted re-learning this (I don't use SendGrid often enough to keep it "top of mind").

Personally, it'd make more sense to keep separate individual properties of "text/plain" and "text/html" content, and then merge them together, in the proper sequence, someplace in the bowels of the library.

Thank you for the detailed feedback @markolbert! Great suggestion, we should be able to bake this into the v9 release.

I just dealt with this I wish I found this error before having to find the solution on my own. haha.

HI @heath-the-monastery,

Glad you found the solution, but sad you had to go through that pain. Thanks for letting us know so we can upvote that issue on your behalf.

This is no longer an issue in v9 of this SDK. Thanks!

Was this page helpful?
0 / 5 - 0 ratings