aspose file tools*
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 Spring in Action this week in the Spring 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
Author

JavaMail set content to utf-8

Kevin Valencia
Greenhorn

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
Greenhorn

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
Greenhorn

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
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

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

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

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

it must be

message.setSubject(subject);

how to encode the subject of the email?

thanks a lot in advance


SCJP 1.4, SCWCD 1.4
amit range
Greenhorn

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");
 
jQuery in Action, 2nd edition
 
subject: JavaMail set content to utf-8