File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Sending JavaMail with Chinese characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Sending JavaMail with Chinese characters" Watch "Sending JavaMail with Chinese characters" New topic
Author

Sending JavaMail with Chinese characters

Jiayu Shao
Greenhorn

Joined: Jan 23, 2014
Posts: 4
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

Joined: Sep 13, 2007
Posts: 2346
    
    7

Have a look at http://www.coderanch.com/t/614198/java/java/Sending-Email-JavaMail-Chinese-Characters#2805926


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Jiayu Shao
Greenhorn

Joined: Jan 23, 2014
Posts: 4
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

Joined: Sep 13, 2007
Posts: 2346
    
    7

File attachment or not an email is the same. The key line(s) is adding a header to the subject and message body.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending JavaMail with Chinese characters