Email sent by JavaMail Gmail cannot restore the file name of the attached file

If you want to send email in Java, you probably use JavaMail.

Recently, the problem that the file name of the attached file cannot be restored by the mail sent by JavaMail and Gmail has come up.

The filename of the attachment is basically specified by the filename parameter in the Content-Disposition header. Some mailers seem to see the name parameter in the Content-Type header, and javamail seems to embed the filename in both.

However, it seems that JavaMail 1.5.3. Has fixed this embedding part.

https://github.com/javaee/javamail/commit/85051272fde868b9fa51e5eb9ca57dcbffffab5a

If the value of the parameter is long, it will now be split according to RFC 2231.

Let's check with a simple program. The first is "70 characters for numbers only" and the second is "30 characters for hiragana only".

ContentDispositionTest.java


import javax.mail.internet.ContentDisposition;
import javax.mail.internet.MimeUtility;

public class ContentDispositionTest {
    public static void main(String args[]) throws Exception {
        ContentDisposition cd = new ContentDisposition("attachment");
        cd.setParameter("filename",
                        "1234567890123456789012345678901234567890123456789012345678901234567890");
        System.out.println(cd.toString());

        cd.setParameter("filename",
                        MimeUtility
                                .encodeText("Aiue Okaki Sashisuseso", "UTF-8", "B"));
        System.out.println(cd.toString());
    }
}

The results of JavaMail 1.5.2 and 1.5.3 are as follows.

:1.5.2


attachment; 
	filename=1234567890123456789012345678901234567890123456789012345678901234567890
attachment; 
	filename="=?UTF-8?B?44GC44GE44GG44GI44GK44GL44GN44GP44GR44GT44GV44GX44GZ44Gb44Gd?="

:1.5.3


attachment; 
	filename*0=123456789012345678901234567890123456789012345678901234567890; 
	filename*1=1234567890
attachment; 
	filename*0="=?UTF-8?B?44GC44GE44GG44GI44GK44GL44GN44GP44GR44GT44GV44GX44"; 
	filename*1="GZ44Gb44Gd?="

Gmail knows that the first can be restored to the correct filename, but the second cannot. (As of 2017-11-21)

If you look at the Content-Disposition header in Gmail's "View Message Source" feature, you'll see that the second is that the parameter values aren't concatenated to their original form.

Content-Disposition: attachment; filename="=?UTF-8?B?44GC44GE44GG44GI44GK44GL44GN44GP44GR44GT44GV44GX44\"; filename*1=\"GZ44Gb44Gd?="

I'm not sure if this JavaMail move is correct as a MIME specification. (Those who understand, I would appreciate it if you could tell me) We know that not only 1.5.3, but the current latest version 1.6.0 will do the same.

By setting the value of the mail.mime.splitlongparameters system property to false, the behavior will be the same as up to 1.5.2.

1.5.3(mail.mime.splitlongparameters=false)


attachment; 
	filename=1234567890123456789012345678901234567890123456789012345678901234567890
attachment; 
	filename="=?UTF-8?B?44GC44GE44GG44GI44GK44GL44GN44GP44GR44GT44GV44GX44GZ44Gb44Gd?="

Added (2017-11-30)

I found it wrong to pass a MIME-encoded string to ContentDisposition # setParameter (). I will summarize what to do at a later date.

Recommended Posts

Email sent by JavaMail Gmail cannot restore the file name of the attached file
Rename the package name of the existing jar file
The attached file name was garbled in the Spring Boot email, so take measures
[Kotlin] Get the argument name of the constructor by reflection
Get the object name of the instance created by the new operator
Change the file name and output destination of the JavaVM error file
Fix the file name of war to the one set in Maven
Overwrite upload of file with the same name with BOX SDK (java)