'MimeMessageHelper encode text does not encode correctly some strings

I'm having a method where the MimeMessageHelper is used to add attachments to the MimeMessage.

To do that, the following code is used :

helper.addAttachment(fileName, new ConstantByteArrayInputStreamSource(doc), contentType);

The weird part is that some of the file names are not correctly inserted. For example, the following name "Action-41080-Cmd_Rappért-20220301_094222.xml" results in the following file name : =UTF-8QAction=5F41080=5FCmd=5FRapp=C3=A9rt=5F20220301=5F= which is obviously not what I want.

So i thought the accent was the issue but when I run with "Action_41080_Cmd_Rappért" it works.

And if I remove the accent (Action-41080-Cmd_Rapport-20220301_094222.xml) it works.

The problem comes from the encodeText part in the MimeUtility which appears to have troubles with this particular string.

Thanks for your time,

If anyone has an idea.



Solution 1:[1]

I think you have to look elsewhere to find the cause of your troubles. A simple test shows that MimeUtility.encodeText works as expected.

import javax.mail.internet.MimeUtility;

// To get required dependency:
// mvn dependency:copy -Dartifact=com.sun.mail:jakarta.mail:1.6.7 -DoutputDirectory=.
public class Q71383383 {

    public static void main(String[] args) {
        
        final String fname = "Action-41080-Cmd_Rappért-20220301_094222.xml";
        
        try {
            var encoded = MimeUtility.encodeText(fname);
            // =?UTF-8?Q?Action-41080-Cmd=5FRapp=C3=A9rt-20220301=5F094222.xml?=
            System.out.println(encoded);
            var decoded = MimeUtility.decodeText(encoded);
            System.out.println(decoded);
            System.out.println(fname);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vanOekel