• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sending Email using JavaMail with Chinese Characters in the email body

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the advice K. Tsang. :-)
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic