JavaMail does not properly encode and decode filenames in non-ASCII character sets

  • Share
  • Share

Applies to:
NA

Description:
Occurs while encoding and decoding filenames in non-ASCII character sets

Cause:
The filename is stored as a parameter in MIME headers. Encoded filenames of the form =?ISO-8859-15?B?5OTkLUluZm8ucGRm?= are not part of the MIME spec. A filename of the form =?A?B?C?= is a perfectly valid filename, not an incorrectly encoded filename. JavaMail does not encoded and decode filenames by default because doing so would violate the MIME spec.
The base MIME spec does not allow for encoding parameters. RFC 2231 defines a new way to include encoded paramters, including filenames, in MIME headers. It is not compatible with the de facto way that many applications illegally encode filenames. Even though JavaMail supports RFC 2231, that alone does not allow JavaMail to interoperate with these existing programs. As far as I know, very few existing programs support RFC 2231.
If you choose to violate the MIME spec, in order to interoperate with other programs that also violate the MIME spec, JavaMail gives you all the tools you need to do so. Starting with JavaMail 1.4, setting the System properties “mail.mime.encodefilename” and “mail.mime.decodefilename” to “true” will cause JavaMail to encode and decode the filename parameter.

JAVA-Mail , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

  1. admin
    March 20th, 2009 at 10:28 | #1

    Applications using earlier versions of JavaMail can use the following workaround to encode a filename:

    mbp.setFileName(MimeUtility.encodeText(filename));

    The workaround for decoding a filename is equally simple:

    String filename = MimeUtility.decodeText(part.getFileName());

  1. No trackbacks yet.