Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaMail set content to utf-8

 
Kevin Valencia
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is the code:


setMimePart method does this:


Result of email content is like this:
�������������������� ������������
 
mohammad ali bafghi zadeh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
amit range
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic