This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes JavaMail set content to utf-8 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JavaMail set content to utf-8" Watch "JavaMail set content to utf-8" New topic

JavaMail set content to utf-8

Kevin Valencia

Joined: Aug 20, 2007
Posts: 18
Hi all,
I'm using javamail to send email notification which contains chinese or japanese characters. Subject line correctly displays chinese characters, however, the email content is garbled. How will i be able to encode the characters in utf-8? I've tried using MimeBodyPart methods such as setContent and setText, but still doesn't work.

Appreciate your help on this.
Kevin Valencia

Joined: Aug 20, 2007
Posts: 18
Below is the code:

setMimePart method does this:

Result of email content is like this:
�������������������� ������������
mohammad ali bafghi zadeh

Joined: Oct 14, 2009
Posts: 1
please use this code :
you can use any character in subject and body.

MimeMessage message = new MimeMessage( session );

message.setFrom( new InternetAddress( from ) );
message.addRecipient( Message.RecipientType.TO, new InternetAddress( toStringAddress ) );
message.addRecipient( Message.RecipientType.BCC, new InternetAddress( bccStringAddress ) );

// Setting the Subject and Content Type
message.setSubject( subject, "utf-8" ); // <----

// Create a message part to represent the body text
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent( messageContent, "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 );
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Seems to me that you are assuming that "theContent" (which I don't see a declaration for) isn't already garbled.
Ali Al Ali

Joined: Dec 29, 2007
Posts: 18
The following line did not work

message.setSubject( subject, "utf-8" );

it must be


how to encode the subject of the email?

thanks a lot in advance

SCJP 1.4, SCWCD 1.4
amit range

Joined: Sep 21, 2011
Posts: 1
I faced same problem but below code resolve

MimeBodyPart bodypart = new MimeBodyPart();
if(emailType.equalsIgnoreCase("text/html")){ //$NON-NLS-1$
bodypart.setHeader("Content-Type","text/plain; charset=\"utf-8\"");
bodypart.setContent( emailBody, "text/plain; charset=utf-8" );
bodypart.setHeader("Content-Transfer-Encoding", "quoted-printable");
I agree. Here's the link:
subject: JavaMail set content to utf-8
It's not a secret anymore!