Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending JavaMail with Chinese characters

 
Jiayu Shao
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2down votefavorite





I have written servlet class and send mail class to send Javamail. The English message works fine, but now I want to send Chinese as well. I suppose you add UTF-8 in MimeMessage's setSubject and setText methods. Email subject and message comes from user input in submit form. I both want subject and message itself accepts Chinese.

Here is my code:

try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);

// Set From: header field of the header.
message.setFrom(new InternetAddress(from));

// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

// Set Subject: header field
message.setSubject(subject, "UTF-8");

// Send the actual HTML message, as big as you like
message.setText(body, "UTF-8");

// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}

I executed the above code, but all chinese characters were corrupted (some really weird characters) in my inbox.

How do I solve encoding problem?

I appreciate if someone could help me.

 
K. Tsang
Bartender
Posts: 3397
13
Android Java
 
Jiayu Shao
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi K.Tsang

I read replies in other questions, but I just need to change the subject and message content to Chinese characters (no file attachment).

Your answer shows how to reading the attachment.

I am not sure what I did wrong.

I appreciate if you can help me.
 
K. Tsang
Bartender
Posts: 3397
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File attachment or not an email is the same. The key line(s) is adding a header to the subject and message body.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic