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:
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);
Tags: Internationalisation, Java, Mail, UTF8
Subscribe to news from Isocra