• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending Email using JavaMail with Chinese Characters in the email body

 
Fer Atas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3457
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make sure the message is in UTF-8, set the header of the messageBody and mime message.

 
Fer Atas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3457
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice K. Tsang. :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic