2015-11-06

Java Mail Attachment with Long File Name and Double Byte Characters

Using JavaMail utility to send email via SMTP, we should encode subject andfile name of attachment if there are double byte characters. It isencoded by the methodMimeUtility.encodeText(text).

The behavior of Java Mail 1.4 and 1.5 isdifferent in handling file name if it is too long. This is the caseof 1.4 in email raw content:
Content-Type: application/octet-stream;
name="=?gb2312?B?w8C5+sjLttShsNK7tPjSu8K3obG1xCDVvSDC1CDLvL+8LTIubW9iaQ==?="
And this is the case of 1.5:
Content-Disposition: attachment;
filename*0="=?GBK?B?w8C5+sjLttShsNK7tPjSu8K3obG1xCDVvSDC1CDLvL+8LTIubW9i";
filename*1="aQ==?="
In order not to avoid the limitation of thelength of each text line, Java Mail 1.5 cuts the file name orencoded file name into multiple lines. However, many emailprocessors or email servers do not handle this new behavior. Thefile is usually renamed as atxxx.octet-stream or ATTxxxxx.dat. Ofcourse, the old case will not always work if the file name is toolong.
If the length is not an issue, be careful toupgrade Java Mail to 1.5.

Labels: , ,