Monday, January 14, 2008

Java Mail Encoding Problems

Well, my first blog entry is related to a programming problem I had. I figured I needed a place to store information about problems I've had to solve, that way if I run into them again, I don't have to hunt around for the solution.

The problem:

I am attempting to send email through Java and the content contains French characters. This content comes from another system, so I don't have any control over the content (i.e. don't have the ability to swap out the French characters with Unicode entity references). I am quite certain that the content I want to send is encoded in UTF-8.I'm also using Spring to help send the email (using MimeMessageHelper and JavaMailSenderImpl).

Originally the email was coming through and displaying garbage characters in place of the French characters in the email client. After some digging around, I discover that the email client thinks the content is ISO-8859-1. If I change the encoding in the client to UTF-8, the characters are displayed correctly.

So with this in mind, I realize I just need to figure out how to tell the email client that the content is UTF-8. Looking at the Spring API I see that I can set the character encoding on the MimeMessageHelper in the constructor. So I do this. The email client is now being told that the content is UTF-8, but now it seems like some kind of encoding translation is happening, because now I'm seeing the garbage characters even though the email client is set to UTF-8.

I then found this post on Java Ranch that seems related. I tried the technique found in that post to set the content - here's the code:


try{
mimeMessageHelper.setText(
new String(extendedMail.getText().getBytes(),"UTF-8"),
extendedMail.isHtml());
} catch (UnsupportedEncodingException e) {
throw new WawanesaException(e.getMessage(), e);
}




And now the content shows up correctly in my email client.