Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use Sendgrid Java API to send UTF-8 encoded emails

I've just switched from Mandrill to Sendgrid. When using the standard Sendgrid JAVA API to send emails the email header content-type is listed as:

Content-Type: text/html; charset=iso-8859-1

I have not been able to find a way to change this to UTF-8 which is my requirement for sending properly encoded emails. This worked just fine when using Mandrill. Sending via Mandrill kept the original source encoding intact in the content-type header. My current code is basically using this Sendgrid sample code: https://sendgrid.com/docs/for-developers/sending-email/v3-java-code-example/

Environment:

  • JDK 1.8
  • sendgrid-java 4.48

My source files are all encoded with UTF-8 and my project is built via Maven with maven-compiler-plugin set to

<encoding>UTF-8</encoding>

The messsage body that I am posting to Sendgrid is HTML and has the following content-type set in the head element:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

The results of using Sendgrid is that the message gets clipped in Gmail. This seems to be due to the conflicting encoding types. Ie the "mail" header says iso-8859-1 even though the actual content is UTF-8. If I understand this correctly the result is that characters are encoded incorretly.

Note: I'm sending Norwegian characters ÆØÅ.

I've tried to convert the message body to ASCII and iso-8859-1. The results of this is that the email is no longer clipped in Gmail, but the ÆØÅ characters are replaced with questionmarks (I know that US_ASCII does not support the ÆØÅ characters). I've also tried to override the Sendgrid header with this code:

mail.addHeader("Content-Type", "text/html; charset=UTF-8");

But that key is reserved and cannot be overriden..

I've also contacted Sendgrid support. They replied with the following:

Unfortunately, our system will automatically encode the body with whatever encoding is being passed to us. This means, that if there are characters in that email that are ISO-8859 characters. Then the body is going to be using that encoding. We do not currently have an option to force UTF-8 encode the body of emails that are being passed through us.

Going forward, you would need to convert these emails previous to sending them to our servers. Or put a procedure in place to decode them into UTF-8 when they are received. For more in depth help with code, I would suggest reaching out of the GitHub issue board of the language you're using is a great way to gain insight: https://github.com/sendgrid/sendgrid-java.

like image 661
Dsleeper Avatar asked May 15 '20 11:05

Dsleeper


1 Answers

As a workaround you can force usage of UTF-8 in HTML email body: add character outside of ISO-8859 charset in comment. For example add:

<!-- ™ -->

Users won't see it unless they open raw body content.

like image 185
Tomasz Sołtysik Avatar answered Nov 15 '22 14:11

Tomasz Sołtysik