hudi
hudi

Reputation: 16555

Decoding name of file attachment doesnt work with properties

I read this documentation:

http://docs.oracle.com/javaee/6/api/javax/mail/internet/package-summary.html

so I add some properties to mimeMessage:

Properties props = new Properties();
        props.put("mail.mime.decodefilename", true);

    Session mailConnection = Session.getInstance(props, null);

    source = new FileInputStream(emlFile);

    MimeMessage message = new MimeMessage(mailConnection, source);

now I am expectaing that method bodyPart.getFileName() return correct name of file. But with this configuration it still doesn work and I need to call mimeUtils: MimeUtility.decodeText - what I dont want. I also try:

        props.put("mail.mime.decodefilename", "true");

but with no success. So what I am doing wrong ?

UPDATE:

after debuging I had this sollution:

this works

    Properties props = System.getProperties();        
    props.put("mail.mime.decodefilename", "true");

this doesnt work:

    Properties props = new Properites();
    props.put("mail.mime.decodefilename", "true");

so if filename is decoding depends on system property too. Does anyone know which properties ? I dont have pattion to try all system properties and solve which one it is

Upvotes: 0

Views: 2508

Answers (1)

user2030471
user2030471

Reputation:

MimeMessage.getFileName

If the mail.mime.encodefilename System property is set to true, the MimeUtility.decodeText method will be used to decode the filename.


Now when one looks at the implementation, this is how the MimeUtility.decodeText comes into picture during the invocation of getFileName:

if (decodeFileName && filename != null) {
    try {
    filename = MimeUtility.decodeText(filename);
    } catch (UnsupportedEncodingException ex) {
    throw new MessagingException("Can't decode filename", ex);
    }
}

Where decodeFileName is initialized like this:

s = System.getProperty("mail.mime.decodefilename");
// default to false
decodeFileName = s != null && !s.equalsIgnoreCase("false");

The javadoc seems to be conflicting with the implementation.

So, try setting mail.mime.decodefilename instead of mail.mime.encodefilename, probably using System.setProperty.

Upvotes: 1

Related Questions