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.
.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.
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:
Thanks for taking the time to share how you came to your solution!
With Best Regards,
Elmer
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!