Sendgrid-csharp: Transactional templated emails' plain text version is shown in email clients when sent through the C# library, while sending tests from SendGrid comes out fine

Created on 16 Mar 2017  路  9Comments  路  Source: sendgrid/sendgrid-csharp

A couple of month ago my transactional templates worked beautifully. I suspect that since the library major version change not so long ago, suddenly the emails are botched up. Instead of showing the HTML version both GMail and Outlook displays the plain text version. Looking into the raw email reveals that there is a HTML version in the email, but that's also somewhat limited, some formatting is gone. Really weird.

Steps to Reproduce

  1. Design a transactional template with SendGrid's editor
  2. Send a a test email using SendGrid's test email functionality -> comes out good
  3. Send the same template from a C# software -> plain text version is shown and even the unrevealed HTML looks limited.

Technical details:

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

        var sendGridApiKey = ConfigurationManager.AppSettings["SendGridApiKey"].ToString();
        var sendGrid = new SendGridClient(sendGridApiKey);
        SendGridMessage mail = new SendGridMessage()
        {
            From = new EmailAddress(emailFrom),
            Subject = subject,
            PlainTextContent = contentStr
        };
        mail.AddTo(new EmailAddress(emaiTo));
        if (!string.IsNullOrEmpty(templateId))
            mail.TemplateId = templateId;
    
        var response = await sendGrid.SendEmailAsync(mail);
    

I'm not sure if the problem is inside the library or on SendGrid's end of the API endpoints, since this was working well 1-2 months ago.

easy help wanted help wanted community enhancement up for grabs up-for-grabs

All 9 comments

Well, this is really interesting. It looks like that if I don't set the PlainTextContent (PlainTextContent = contentStr) while creating a SendGridMessage for a transactional templated email (later mail.TemplateId = templateId;) then it works fine. Some kind of side effect of some API calls. Note, that I'm not setting the content as in the example (with AddContent(MimeType.Text) I'll try that too. I noticed I'm also setting the TemplateId with an assignment instead of a SetTemplateId.

I'm setting the PlainTextContent BTW because this code is in the core of my email sending service and it's used for both transactional templated and non templated emails.

Eve if I follow the example better it comes out plain text:

        SendGridMessage mail = new SendGridMessage();
        mail.SetFrom(new EmailAddress(emailFrom));
        mail.SetSubject(subject);
        mail.AddTo(new EmailAddress(emaiTo));
        mail.AddContent(MimeType.Text, contentStr);
        if (!string.IsNullOrEmpty(templateId))
            mail.SetTemplateId(templateId);

The only way it works as intended now is if I don't add a Content when sending templated email. Which kinda makes sense, because it'll be replaced anyway by the template, but it's kinda strange that this code was working before and now it doesn't work as expected.

Hello @MrCsabaToth,

v9 was a complete breaking change rewrite. I'm glad you figured it out, but I would like to better understand how we can make this process easier.

If you have a moment, could you please answer these questions:

  1. Does your template have a <%body%> tag? I'm guessing that if it does not, when you set the content, it overwrites the template. I'll need to test this.
  2. Did you follow the example here: https://github.com/sendgrid/sendgrid-csharp/blob/master/USE_CASES.md#transactional_templates If not, why not? If so, what went wrong?
  3. Is this a correct assumption: If you are using a templateID, you would like the Content objects to be automatically dropped, unless there is a <%body%> tag in your template?

Thanks for taking the time to share how you came to your solution!

With Best Regards,

Elmer

  1. It doesn't have. I only personalize it by some template parameters.
  2. I don't know how I ended up with my code, I mixed it together from multiple sources I believe. What goes wrong is if I set plain text Content for a template without a <%body%> tag, the Content is overwritten by the template, but the resulting email is weird: comes out plain text in the email client.
  3. What I expect is that regardless if I set the plain text Content or not for a template without <%body%> tag, the resulting email should come out the same in the end (since the Content will be replaced anyway). Right now when I set something to the plain text Content the resulting email is kinda botched up (showed as plain text by the client).

Thanks @MrCsabaToth,

This is great feedback, I will use it to craft some failing tests to create a fix against. I'm leaving this ticket open for that purpose.

With Best Regards,

Elmer

I was having the same issue. Then I discovered that if you change the MimeType from Text to Html it works.

So if using the above example:
C# SendGridMessage mail = new SendGridMessage(); mail.SetFrom(new EmailAddress(emailFrom)); mail.SetSubject(subject); mail.AddTo(new EmailAddress(emaiTo)); mail.AddContent(MimeType.Html, contentStr); if (!string.IsNullOrEmpty(templateId)) mail.SetTemplateId(templateId);

And even if you are not supplying content to your email, you still need mail.AddContent(MimeType.Html, "") for the template to render as html.

Not sure if this fixes your problem, but it worked for me

@matthewnitschke I'll try that, it cannot hurt anyway

Thanks for the additional feedback @matthewaltman!

When we get this sorted, I will update the example here.

Since there has been no activity on this issue since March 1, 2020, we are closing this issue. Please feel free to reopen or create a new issue if you still require assistance. Thank you!

Was this page helpful?
0 / 5 - 0 ratings