This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes Sending Email using JavaMail with Chinese Characters in the email body Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Sending Email using JavaMail with Chinese Characters in the email body" Watch "Sending Email using JavaMail with Chinese Characters in the email body" New topic
Author

Sending Email using JavaMail with Chinese Characters in the email body

Fer Atas
Greenhorn

Joined: Jan 19, 2005
Posts: 11
I am quite new to Javamail and its implementations options.

I am able to send email using it with a variety programs but was unable to do so when including chinese characters as part of the email body, in HTML format.
The email I am trying to send is using a template (txt file) that is saved under UTF-8 encoding. I have tried various steps from the internet and wasn't able to get what I expect to receive.
The chinese characters are either displayed as "???" or "親愛的客戶:". But in the subject line, I was able to display the chinese characters properly

Below is some part of my code:

public boolean sendEmailNotice (String from, String[] to, String subject)
{
try {

InternetAddress[] toList = new InternetAddress[to.length];
toList[0] = new InternetAddress(to[0]);

currentMessage.setFrom(new InternetAddress(from));
currentMessage.setRecipients(Message.RecipientType.TO, toList);
currentMessage.setSubject(subject, "utf-8");
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setText(emailBody, "utf-8", "html");
multipart.addBodyPart(mimeBodyPart);
currentMessage.setContent(multipart);
currentMessage.setSentDate(new Date());
currentMessage.saveChanges();
Transport.send(currentMessage);

} catch (Exception gEx) {
gEx.printStackTrace();
}
return false;
}

=================================================================
// this section is being called separately in the main program before execute the sendEmailNotice to format the emailBody

public String setupEmailContent (Object[] text) {
emailBody = MessageFormat.format(templateStr , text);
return emailBody;
}


Any help will be greatly appreciated.

Thanks.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2242
    
    7

To make sure the message is in UTF-8, set the header of the messageBody and mime message.



K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Fer Atas
Greenhorn

Joined: Jan 19, 2005
Posts: 11
Thanks K. Tsang.

I have already tried doing your suggestion but wasn't able to fix the problem. But now, I am able to make my program work. The problem actually occurs not in the emailing part but starts from the process of reading the content of the Template in UTF-8 format.

I tried rewriting the file and produced the same results with the garbled email.

What I did was to use a FileInputStream and an InputStreamReader for me to specify the encoding during the process of reading the template. =)
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2242
    
    7

Ah yes, when reading the file, it's better to specify the encoding. Better yet, the original source come in UTF-8

To check for UTF-8 you can try this:
Fer Atas
Greenhorn

Joined: Jan 19, 2005
Posts: 11
Thanks for the advice K. Tsang. :-)
 
Don't get me started about those stupid light bulbs.
 
subject: Sending Email using JavaMail with Chinese Characters in the email body
 
Similar Threads
JavaMail set content to utf-8
jsp mails in tomcat
Problem in Send mail to MS Outlook with hyperlink body using java Mail API
JavaMail API error
javamail API question: How to send content type in message body when using multiparts