Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Character Encoding with JavaMail

843834Sep 8 2009 — edited Sep 16 2009
Having an issue moving from Windows to Linux RH5. I assume there is something I am not configuring properly. Your help is appreciated.

Environment
- Locally, Windows XP SP3, JVM = Classic VM (build 1.4.2, J2RE 1.4.2 IBM Windows 32 build cn142-20080515 (SR11) (JIT enabled: jitc)), JavaMail 1.3.1_02
- Dev box, RH5 = 2.6.18-164.el5PAE, JVM = Classic VM (build 1.4.2, J2RE 1.4.2 IBM Windows 32 build cn142-20080515 (SR11) (JIT enabled: jitc)), JavaMail 1.3.1_02
- Email server: Postfix

The code that is being executed
MimeMultipart mp = new MimeMultipart("alternative");
for (Iterator itParts = emailDTO.getAlternates().iterator(); itParts.hasNext();){
  MultipartAlternateDTO alternateDTO = (MultipartAlternateDTO) itParts.next();
  MimeBodyPart bp = new MimeBodyPart();
  bp.setDisposition(Part.INLINE);
  bp.setContent(alternateDTO.getContent(), alternateDTO.getContentType() + "; charset=\"utf-8\"");
  bp.setHeader("Content-Type",alternateDTO.getContentType() + "; charset=\"utf-8\"");
  bp.setHeader("Content-Transfer-Encoding", "quoted-printable");
  mp.addBodyPart(bp);
  if (TRACING) TRACE.debug("Content for Email: " + alternateDTO.getContent());
}

String languageId = Constants.GLOBAL_STORE_LANG_ID.toString();
if (emailDTO.getLanguageId()!=null){
  languageId = emailDTO.getLanguageId().toString();
}
		
Properties mailProps = new Properties();
mailProps.setProperty("mail.smtp.host", "smtp.dvlp-XXX.com");
Session mailSess = Session.getDefaultInstance(mailProps);
MimeMessage message = new MimeMessage(mailSess);
message.setContentLanguage(new String[] {languageId});
message.setFrom(new InternetAddress(emailDTO.getFrom()));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailDTO.getTo()));
if (StringUtils.isNotBlank(emailDTO.getCc())) 
  message.addRecipient(Message.RecipientType.CC, new InternetAddress(emailDTO.getCc()));
if (StringUtils.isNotBlank(emailDTO.getBcc())) 
  message.addRecipient(Message.RecipientType.BCC, new InternetAddress(emailDTO.getBcc()));
if (StringUtils.isNotBlank(emailDTO.getSubject()))
  message.setSubject(MimeUtility.encodeText(emailDTO.getSubject(), "utf-8", "B"));
message.setContent(mp);
try { 
  Transport.send(message);
  messageSent = true;
} catch (MessagingException me) {
  messageSent = false;
  LOG.error("An error occurred while sending the multipart email: " + me.getMessage(), me);
}
Problem

When I run this code on the local Windows environment, I recieve a properly encoded subject and body of the email. In the headers, I see UTF-8 encoding:
Microsoft Mail Internet Headers Version 2.0
...
Message-ID: <1672102924.1252438814957.JavaMail.XXX>
Subject: =?utf-8?B?Vm90cmUgY29tbWFuZGUgbGEgTGlicmFpcmk=?=
 =?utf-8?B?ZSBkZSBsJ1VuaXZlcnNpdMOpIGQnT3R0YXdh?=
Mime-Version: 1.0
Content-Type: multipart/alternative; 
	boundary="----=_Part_226_169935885.1252438814902"
Content-Language: -2
Date: Tue,  8 Sep 2009 14:40:14 -0500 (CDT)

------=_Part_226_169935885.1252438814902
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
------=_Part_226_169935885.1252438814902
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
------=_Part_226_169935885.1252438814902--
But when I run this same code on the Linux box, I see that the subject is encoded properly but the body is not. There are no special characters at all.

Windows Sample: Résumé de la commande
Linux Sample: Rumde la commande

What Ive Tried

I assumed this was an issue right away with the Linux configuration. First, I assured the Linux LANG environment variable was set properly, and it is set to en_US.UTF-8. Next, I added the JVM property for encoding as: "-Dmail.mime.charset=UTF-8". Finally, I tried "-Dfile.encoding=UTF-8".
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 14 2009
Added on Sep 8 2009
3 comments
778 views