Our application takes text from a web form and sends it via email to an appropriate user. However, when someone copy/pastes in the infamous "smart quotes" or other special characters from Word, things get hairy.
The user types in
he said “hello” to me—isn’t that nice?
But when the message appears in Outlook 2003, it comes out like this:
he said hello to meisnt that nice?
The code for this was:
Session session = Session.getInstance(props, new MailAuthenticator());
Message msg = new MimeMessage(session);
//removed setting to/from addresses to simplify
msg.setSubject(subject);
msg.setText(text);
msg.setHeader("X-Mailer", MailSender.class.getName());
msg.setSentDate(new Date());
Transport.send(msg);
After a little research, I figured this was probably a character encoding issue and attempted to move things to UTF-8. So, I updated the code thusly:
Session session = Session.getInstance(props, new MailAuthenticator());
MimeMessage msg = new MimeMessage(session);
//removed setting to/from addresses to simplify
msg.setHeader("X-Mailer", MailSender.class.getName());
msg.addHeader("Content-Type", "text/plain");
msg.addHeader("charset", "UTF-8");
msg.setSentDate(new Date());
Transport.send(msg);
This got me closer, but no cigar:
he said “hello” to me—isn’t that nice?
I can't imagine this is an uncommon problem--what have I missed?