theyuv
theyuv

Reputation: 1624

JavaMail file attachment in non-latin not displayed properly

I am using JavaMail (version 1.5) in a web application.

Users upload files via an file input element on an html form.

I then send the file via email. I use the same name as the file that was attached via the form (ie: I don't give it some custom name).

Some users upload files with non-latin file names.

According to docs I understand that no special encoding/decoding is needed with JavaMail unless I'm manipulating raw headers. Therefore I set the file name of the attachment via mimeBodyPart.setFileName(fileName) without encoding the file name.

However, this results in the file name showing up as question marks or underscores in the received email. Testing printing fileName to the console or to a System folder displays the file name correctly.

Things I've tried

  1. I tried setting "mail.mime.encodefilename", and "mail.mime.decodefilename" to true in the app's ServletContextListener.

  2. I tried using the MimeUtility method encodeText(text,charset, encoding).

This worked and displayed the file name correctly but occasionally it would completely garble the file name: rather than question marks or underscores, it would garble the entire file name to look something like "=_UTF-8_B_16rXqNeS15XXnSDXnNeg15zXlCAt157XmdeV157XoNeV16og15___ ___filename_1=__4..."

I did extensive testing to try to figure out what caused this garbling but couldn't get consistent results (tested for browsers and file name length). I suspect something was getting cached during testing because the results weren't consistent.

  1. I tried getting the file name both from the Java Part method getSubmittedFileName() and via a js script.

Any suggestions?

Sample headers:

Content-Type: application/x-any; name="=?UTF-8?B?16TXlCDXoteV15Mg157Xmdec15nXnSDXnA==?= =?UTF-8?B?1\"; name*1=\"5DXqNeV15og157Xodek16guZG9jeA==?=" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="=?UTF-8?B?16TXlCDXoteV15Mg157Xmdec15nXnSDXnA==?= =?UTF-8?B?1\"; filename*1=\"5DXqNeV15og157Xodek16guZG9jeA==?="

Clients used to view received emails: Gmail website, Microsoft Outlook.

Upvotes: 1

Views: 753

Answers (1)

champion44
champion44

Reputation: 1

I was facing the same issue and able to resolve it with java-mail-api-1.6. It doesn't seems standard but it can be useful if you facing such issues.

The other options like using:

attachment.setFileName(MimeUtility.encodeText(FileName, "UTF-8", null));

was not performing well with different mail servers; results were random and confusing.

Below are steps that I used:

Step 1: Set following properties:

props.put("mail.mime.encodefilename", "true");
props.put("mail.mime.encodeparameters", "false");

Step 2: Set file name with:

attachment.setHeader("Content-Type", "application/octet-stream; 
name=\""+FileName+"\"");
attachment.setHeader("Content-Disposition", "attachment; 
filename=\""+FileName+"\"");

Then remove other filename setting statements like

attachment.setFileName(FileName);

Upvotes: 0

Related Questions