I am using below code to send mail to multiple users across different domains.
String emailBody = "<html>Some html text goes here</html>"
String from = "from@domain1.com"
String bcc = "bcc@domain2.com"
String subject = "Some subject..."
List attachments = "Some image attachments ..."
MimeMessage message = new MimeMessage(session);
message.setHeader("Content-Type", "text/html; charset=UTF-16")
message.setHeader("Accept-Language", "en-US")
message.setHeader("Content-Language", "en-US")
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(bcc));
message.setSubject(subject);
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(emailBody, "text/html; charset=UTF-16");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
addAttachmentsToMultiPart(multipart,attachments)
message.setContent(multipart);
Transport.send(message);
The email body is entirely is in English. But users of few domains are recieving it in Chinese characters. When I viewed the html source of the body, I found it is partially garbled. Some orginal html text is intact and some junk characters in between text with no particular order. This mail travels this path "Tomcat application -> company mail server -> outlook.office365.com server -> target mail server -> End user outlook client".
After some research, I found this issue could be due to Character encoding. For testing it, I tried sending this mail to gamil.com. The mail body appears correctly in gmail, but I notice following headers are found in headers part and entire body part is base64 encoded.
Content-Type: text/html; charset="UTF-16"
Content-Transfer-Encoding: base64
I am not sure how "Content-Transfer-Encoding" is being set on headers and where is mail body getting encoded. But I don't see similar headers in End User mail properties. I tried explicitly setting "Content-Transfer-Encoding" to "base64", "quoted-printable" ( and other types), but it is always ignored and gmail always recieves it as base64. And this setting has no impact end user mails. They always recieves same garbage.
Can anyone help me understand what could be issue and how to resolve it