Tag Archives: Internationalisation

UTF-8 subjects in javax.mail

I’ve been having problems with putting unicode strings into the subject of emails sent using javax.mail.

In the end, the solution was very simple and I found it here:

http://www.velocityreviews.com/forums/t132009-utf8-characters-not-appearing-correctly-in-email-subject-line.html

As long as you use “UTF-8” and not “UTF8” it all seems to work fine, so the code you need is:

Properties props = new Properties();
// put in your SMTP host in here
props.put("mail.smtp.host", "localhost");
Session s = Session.getInstance(props, null);

MimeMessage message = new MimeMessage(s);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject, "UTF-8");
message.setText(body, "UTF-8");
message.setHeader("Content-Type", "text/plain; charset=UTF-8");
Transport.send(message);

Internationalisation again

Further to my post about creating properties files when I had text files in unicode, we decided that the best thing was to have a web application to do the conversion. You can then just paste in whatever text you want to encode and it tells you what the java escaped text string(s) should be.

And, we decided we might as well make it publicly available. So here it is.

Let us know if you find any bugs or if you can think of a better way to do it :-)