Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding HTML Content Before Plain Text Content Results In Bad Request Response #352

Closed
brendonwm opened this issue Oct 18, 2016 · 6 comments
Labels
status: help wanted requesting help from the community type: bug bug in the library

Comments

@brendonwm
Copy link

brendonwm commented Oct 18, 2016

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.
@thinkingserious thinkingserious added type: bug bug in the library status: help wanted requesting help from the community labels Oct 19, 2016
@thinkingserious
Copy link
Contributor

Thanks for the heads up @brendonwm!

@markolbert
Copy link

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.

@thinkingserious
Copy link
Contributor

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

@heath-the-monastery
Copy link

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

@thinkingserious
Copy link
Contributor

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.

@thinkingserious
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: bug bug in the library
Projects
None yet
Development

No branches or pull requests

4 participants