Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

The attached file name is converted to .dat in outlook (on Japanese system locale) while sending ema

838162Jun 7 2017 — edited Jul 3 2017

I am seeing weird behavior after upgrade the javamail API to 1.5.6 from 1.4.1. Email message is sent which has the attachment file name with multibyte characters (e.g. (代替機ログ)show tech-support.zip ) using 1.5.6, the email received in outlook which is installed on windows system in Japanese locale (please note the attachment appears correctly in thunderbird client in this scenario i.e. in Japanese locale with 1.5.6), the attachment file name gets converted into .dat extension (e.g. 10224.dat), while the attachment appear correctly when email is sent using javamail API version 1.4.1. We have seen the API has changed between 1.5.6 and 1.4.1, though the API 1.5.6 is backward compatible with 1.4.1, the email message with attachment that has multibyte characters is sent using 1.5.6, the outlook in English locale fails to parse the attachment file name and shows raw encoded data (e.g. ="=?utf8=?=…..) and then we used the below method and it at least worked in outlook installed in English locale.

Method used using 1.5.6 version – MimeBodyPart attachFile(…).

In the same decoded eml message data, I could see the difference in encoding raw string when email sent using version 1.5.6 and 1.4.1.

The file name encoding string in message structure using the API versions is as below.

  • Attachment part of email sent using version 1.5.6 (failed in outlook on Japanese system locale) –

Content-Type: application/octet-stream;

name="=?utf-8?B?54Sh6aGM44Gu5re75LuY44OV44Kh44Kk44OrIDAwNTUzLmRhdA==?="

Content-Transfer-Encoding: base64

X-Rtf-RenderingPosition: -1

Content-Disposition: attachment;

filename="=?utf-8?B?54Sh6aGM44Gu5re75LuY44OV44Kh44Kk44OrIDAwNTUzLmRhdA==?="

  • Attachment part of email sent using version 1.4.1 (successful in outlook on Japanese system locale) –

Content-Type: application/octet-stream;

name="=?utf-8?B?KOS7o+abv+apn+ODreOCsClzaG93IHRlY2gtc3VwcG9ydC56aXA=?="

Content-Transfer-Encoding: base64

X-Rtf-RenderingPosition: -1

Content-Disposition: attachment;

filename="=?utf-8?B?KOS7o+abv+apn+ODreOCsClzaG93IHRlY2gtc3VwcG9ydC56aXA=?="

Please help me to troubleshoot and resolve this problem.

Comments

Bill Shannon-Oracle

There's several variables here and it's hard to tell which one is causing the problem.

First, JavaMail 1.5.6 supports some newer internet email standards by default, which some versions of Outlook do not support.  See the mail.mime.encodeparameters System property.

However, it doesn't appear that your filename is being encoded using that method.

You say you're using the MimeBodyPart.attachFile method, so I assume that means you're not setting the filename separately, is that correct?

You don't say whether you're setting the mail.mime.encodefilename property, yet from your examples it appears that the filename is being encoded using that non-standard method.  Are you setting that property?

There's no obvious difference in the format of your example messages between 1.4.1 and 1.5.6, although clearly the filename content is different.  Are you running both programs on the same machine with the same JDK and the same operating system locale setting?  Are those examples of the messages as they're sent, or as they're received?  If you're using Exchange, it will often reformat the message to what it prefers, which may be introducing the error.

Try setting the mail.mime.encodeparamters property to false, and if that doesn't solve the problem use something like msg.writeTo(new FileOutputStream("msg.txt")) before sending the message to save a copy of the message as it's being sent, to compare with the message as it's received.

838162

Hi Bill,

Thanks for your reply on this, below is the answers to your question

  • You say you're using the MimeBodyPart.attachFile method, so I assume that means you're not setting the filename separately, is that correct?

[Answer]: I tried both i.e. without setting the file name and setting the file name explicitly (which I did by default with 1.4.1 ) using method setFileName(MimeUtility.encodeText(….,….., null) but result is same.

  • You don't say whether you're setting the mail.mime.encodefilename property, yet from your examples it appears that the filename is being encoded using that non-standard method.  Are you setting that property?

[Answer]: I did not set this property in any of my test.

  • There's no obvious difference in the format of your example messages between 1.4.1 and 1.5.6, although clearly the filename content is different.  Are you running both programs on the same machine with the same JDK and the same operating system locale setting?  Are those examples of the messages as they're sent, or as they're received?  If you're using Exchange, it will often reformat the message to what it prefers, which may be introducing the error.

[Answer]: The environment is same for both 1.4.1 and 1.5.6, I just switch to the javamail library in my environment. These messages as they are received.

I tried setting the property “mail.mime.encodeparamters” to false and msg.writeTo(new FileOutputStream("msg.txt")) but no change in behavior.

Bill Shannon-Oracle

Sorry, typo in the property name.  It should be mail.mime.encodeparameters.  Try that.  Then post the msg.txt file for 1.5.6.  I assume you are not calling setFileName for 1.5.6, right?

I suspect there's some detail that you're not getting quite right, but it's hard to guess what without seeing all the details.  You might try modifying the JavaMail sendfile.java sample program to reproduce the problem, then post the modified program and the JavaMail debug output.

838162

Hi Bill,

I tried this parameter "mail.mime.encodeparameters" and still seeing the same behavior and even the attachment file names are not received correctly on any client running on any locale (English, Japanese) after setting this parameter to false. I am not using setFileName for 1.5.6, and my program is similar to the sample program you pointed.

Bill Shannon-Oracle

Sorry, I need more details to figure out what's going wrong.  Rather than post them here, please send the details to me at javamail_ww@oracle.com.  I need:

A modified version of sendfile.java that reproduces the problem.

Whatever input the program requires to reproduce the problem.

Send these as attachments to your message.

If you can send mail using JavaMail 1.4.1 that displays properly in Outlook, we should be able to figure out how to send the same message using JavaMail 1.5.6 so that it will also display properly.

1310163

Hey Bill,

We have the same problem and found, the following out:

- With Outlook Express 6, Outlook 2007 SP3 and Outlook 2010 the problem occurs, if the

  attachment name is longer than 60 bytes, with the JavaMail API 1.5.6!

- Also we found out that the exchange-backend can influence the problem as well.

  In our actual infrastructure with newest Versions of Microsoft Exchange (server and client), the problem does not occur.

The older Version 1.4.1 is working fine. (Also Tests with 1.4.7 are working).

kindly regards,

Felix

Bill Shannon-Oracle

Newer versions of JavaMail use newer MIME standards to encode parameters such as the file name.

These newer standards allow the parameter to be split into pieces.  I strongly suspect those versions

of Outlook are not supporting these newer MIME standards, or are not supporting them properly.

You can disable this in newer versions of JavaMail by setting "mail.mime.encodeparameters" to false,

as described above.  If this isn't working, I need more details to determine why.

This "newer" standard (RFC 2231) is now 20 years old.  Outlook really should be supporting it by now.

1310163

Hey Bill,

Thank you very much for the advice.

We tried that. But it does not solve the problem, still the same behaviour.

email body, attachement file name is: Pink Pallin, 01.01.1970, PID-777777, FID-8888888, AID-99X.pdf

length: 61

mailproperties: {mail.smtp.starttls.enable=true, mail.smtp.port=587, mail.smtp.host=smtpauth.bluewin.ch, mail.smtp.auth=true, mail.mime.encodeparameters=false}

The attachment here is called: ATT00010.dat

kindly regards, Felix

Bill Shannon-Oracle

As I said, I need more details.  Can you create a modified version of the sendfile.java program (linked to above) that demonstrates the problem, and run it with both 1.4.1 and 1.5.6 and send me the debug output?

Here's what I get using sendfile.java to send your sample file name:

By default using JavaMail 1.5.6, I get these headers:

Content-Type: application/octet-stream;

        name*0="Pink Pallin, 01.01.1970, PID-777777, FID-8888888,

AID-99X.pd"; name*1=f

Content-Transfer-Encoding: 7bit

Content-Disposition: attachment;

        filename*0="Pink Pallin, 01.01.1970, PID-777777, FID-8888888,

AID-99X.pd"; filename*1=f

When I set mail.mime.encodeparameters to false, I get these headers:

Content-Type: application/octet-stream;

        name="Pink Pallin, 01.01.1970, PID-777777, FID-8888888, AID-99X.pdf"

Content-Transfer-Encoding: 7bit

Content-Disposition: attachment;

        filename="Pink Pallin, 01.01.1970, PID-777777, FID-8888888,

AID-99X.pdf"

When I run with JavaMail 1.4.1, I get these headers:

Content-Type: application/octet-stream;

        name="Pink Pallin, 01.01.1970, PID-777777, FID-8888888, AID-99X.pdf"

Content-Transfer-Encoding: 7bit

Content-Disposition: attachment;

        filename="Pink Pallin, 01.01.1970, PID-777777, FID-8888888,

AID-99X.pdf"

As you can see, the 1.4.1 and 1.5.6 headers are the same in this case.

I can't imagine why Outlook would display the messages differently if

the headers are the same.

If you're getting different results, I need a reproducible test case.

1310163

Hey Bill,

Here the information you requested:

To reproduce Issue:

  • Edit: Replace "TBD" in TestMailIssue.java with necessary mail settings/credentials
  • Copy javax.mail-1.5.6.jar in same directory of TestMailIssue.java and PDF document
  • Compile:  # javac -cp ".;javax.mail-1.5.6.jar" TestMailIssue.java
  • Run: # java -cp ".;javax.mail-1.5.6.jar" TestMailIssue

PDF File:

  • PDF Document to test mail issue, with filname longer than 60 and mail api 1.5.6.pdf

Environment details:

Mail sent from:

OS: Windows 10 (Enterprise)

JDK: 1.8.0_131

Mail received by:

OS: Windows 7

Mail client: Outlook 2007 (Note: with modern Mail Clients works!)

Incoming Mail server: POP3 or IMAP

Java Code:

import java.io.File;

import java.util.Properties;

import javax.activation.DataHandler;

import javax.activation.DataSource;

import javax.activation.FileDataSource;

import javax.mail.Address;

import javax.mail.BodyPart;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.Part;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

public class TestMailIssue {

   

    private    static final String SENDER         = "TBD";

    private    static final String RECEIVER         = "TBD";

    private    static final String SMTP_HOST         = "TBD";

    private    static final String SMTP_HOST_PORT     = "TBD";       

    private    static final String SMTP_HOST_PWD     = "TBD";

       

        private    static final String PATH_FILE_NAME         = "PDF Document to test mail issue, with filname longer than 60 and mail api 1.5.6.pdf";

   

        // WORKS

        //private    static final String BODY_PART_FILE_NAME = "PDF Document to test mail issue, with filname longer tha.pdf";

        // DOESN'T WORK

        private    static final String BODY_PART_FILE_NAME = "PDF Document to test mail issue, with filname longer than 60.pdf";

   

    public static void main(String[] args) throws AddressException, MessagingException {

       

        Properties props = new Properties();

                               

        props.put("mail.smtp.auth",             "true");

        props.put("mail.smtp.starttls.enable",     "true");

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

        props.put("mail.smtp.host",             SMTP_HOST);

        props.put("mail.smtp.port",             SMTP_HOST_PORT);

       

        Session mailSession = Session.getDefaultInstance(props,  new javax.mail.Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {

                return new PasswordAuthentication(SENDER, SMTP_HOST_PWD);

            }

        });

       

        Message message = new MimeMessage(mailSession);   

        message.setFrom(new InternetAddress(SENDER));

        message.addRecipient(Message.RecipientType.TO, new InternetAddress(RECEIVER));

        message.setSubject("test mail issue - java mail version:" + message.getClass().getPackage().getImplementationVersion());

                       

            BodyPart messageBodyPart = new MimeBodyPart();

       

            StringBuilder text = new StringBuilder();

            text.append("email body, attachement file is: ");

            text.append(PATH_FILE_NAME);

            text.append("\n");

            text.append("used file name: ");

            text.append(BODY_PART_FILE_NAME);

            text.append("\n");

            text.append("file name length: ");

            text.append(BODY_PART_FILE_NAME.length());

            text.append("\n");

            text.append("mail properties: ");

            text.append(props.toString());

            text.append("\n");

            messageBodyPart.setText(text.toString());

       

            Multipart multipart = new MimeMultipart();

       

            multipart.addBodyPart(messageBodyPart);

            File myFile = new File(PATH_FILE_NAME);

            DataSource source = new FileDataSource(myFile.getAbsolutePath());

            messageBodyPart = new MimeBodyPart();       

            messageBodyPart.setDataHandler(new DataHandler(source));

       

            messageBodyPart.setFileName( BODY_PART_FILE_NAME);

               

            multipart.addBodyPart(messageBodyPart);

           

            message.setContent(multipart);

        Transport.send(message);

       

    }

}

Bill Shannon-Oracle

mail.mime.encodeparameters is a System property, not a Session property.

System.setProperty("mail.mime.encodeparameters", "false");

1310163

Thanks Bill. That is working. :-)

838162

Hi Bill,

My program is exactly the same which you have pointed in your earlier email i.e. https://github.com/javaee/javamail/blob/master/demo/src/main/java/sendfile.java . The attachment file name has Japanese characters "e.g. 金融監督院 rismytesttoZip.zip".in my program, the file name appears correct in outlook installed on English locale, and the only problem in outlook which installed on Windows with Japanese language only.

1 - 13
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 31 2017
Added on Jun 7 2017
13 comments
9,556 views