This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Learn Java with Math: Using Fun Projects and Games and have Ron Dai on-line!
See this thread for details.
Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

JavaMail set content to utf-8

 
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:
�������������������� ������������
 
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 );
 
Sheriff
Posts: 24715
59
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.
 
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
 
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");
 
Skool. Stay in. Smartness. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!