Email does not work with Microsoft Outlook, but works fine with web emails.
877458Jul 26 2011 — edited Jul 29 2011This is what I am doing:
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage( session );
message.setHeader("Content-Type", "text/plain; charset=UTF-8");
message.setFrom( new InternetAddress( from ) );
message.addRecipient( Message.RecipientType.TO, new InternetAddress( to ) );
//message.addRecipient( Message.RecipientType.BCC, new InternetAddress( bccStringAddress ) );
// Setting the Subject and Content Type
//message.setSubject( subject); // <----
//message.setSubject(subject, "text/plain; charset=utf-8");
//message.setSubject(subject, "UTF-8");
message.setSubject(MimeUtility.encodeText(subject, "UTF-8", "B"));
// Create a message part to represent the body text
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent( content, "text/html; charset=utf-8" ); // <----
// use a MimeMultipart as we need to handle the file attachments
Multipart multipart = new MimeMultipart();
// add the message body to the mime message
multipart.addBodyPart( messageBodyPart );
// Put all message parts in the message
message.setContent( multipart );
Transport.send( message );
It looks like the email it sends out work fine with web based email tools, such as hotmail.com or gmail.com. However, it does not work well in Microsfot Outlook, where the subject is fine but the body of the email gets corruptted. I got a lot of square dots as a replacement as Chinese words. I tried both 问候 and \u95ee\u5019.
Since it is still a problem of unicode, I think it might suit for this thread. But if you don't think so, I can open a new thread.
Any suggestions?