Search code examples
javaemailsmtpjakarta-mailapache-commons-email

Why attachment file name broken when using the apache-email library?


The mail was send using the apache-email library.(commons-email-1.5.jar) If the Korean language is included in the attachment file name, the attachment file name is broken. What is the way for filename to be sent normally? Do not use javax.mail-api library directly. only should use apache-email library.


try this

  • setting disposition (attachment, inline)
  • setting encoding (utf-8, euc-kr)
  • using javax.mail-api library (It works fine but should not be used.)

source code

using commons-email-1.5.jar (attachment file name is broken)
// using commons-email-1.5.jar)
String fileName = "한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글.txt";
String filePath = "./file/";
MultiPartEmail email = new MultiPartEmail();
email.setCharset("UTF-8");
email.setSmtpPort(465);
email.setHostName("xxx");
email.addTo("xxx");
email.setFrom("xxx");
email.setSubject("Message Alert");
email.setMsg("This is message body");
email.setAuthenticator(new DefaultAuthenticator("xxx", "xxx"));
email.setSSLOnConnect(true);
email.setDebug(true);

/** case 1 */
EmailAttachment attachment = new EmailAttachment();
attachment.setName(fileName);
attachment.setPath(filePath + fileName);
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription(fileName); 
email.attach(attachment);
email.send();
using javax.mail-api.jar (attachment file name is not broken)
String to = "xxx";// change accordingly
final String user = "xxx";// change accordingly
final String password = "xxx";

// 1) get the session object
Properties props = new Properties();
props.put("mail.smtp.host", "xxxxxx");
props.put("mail.smtp.port", "25");
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.EnableSSL.enable", "true");
props.put("mail.mime.encodeparameters", "false");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");

Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password);
    }
});

// 2) compose message
try {
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(user));
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    msg.setSubject("Message Alert");

    // 3) create MimeBodyPart object and set your message text
    BodyPart messageBodyPart1 = new MimeBodyPart();
    messageBodyPart1.setText("This is message body");

    // 4) create new MimeBodyPart object and set DataHandler object to this object
    MimeBodyPart messageBodyPart2 = new MimeBodyPart();

    String filePath = "./file/한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글.txt"; // change accordingly
    String fileName = "한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글.txt";
    DataSource source = new FileDataSource(filePath);
    messageBodyPart2.setDataHandler(new DataHandler(source));
    messageBodyPart2.setFileName(fileName);

    // 5) create Multipart object and add MimeBodyPart objects to this object
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart1);
    multipart.addBodyPart(messageBodyPart2);

    // 6) set the multiplart object to the message object
    msg.setContent(multipart);

    // 7) send message
    Transport.send(msg);

    System.out.println("message sent....");
} catch (MessagingException ex) {
    ex.printStackTrace();
}

actual results

using commons-email-1.5.jar - file name broken.
attachment file name : =_UTF-8_Q_=ED=95=9C=EA=B8=80testfile=ED=95=9C=EA=B8=80_= =_UTF-8_Q_=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80testfil_= =_UTF-8_Q_e=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80=E___ ___filename_3=__D=95=9.txt_=
using javax.mail-api.jar - file name normal
attachment file name : 한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글.txt

email send log

log using commons-email-1.5.jar in debuging mode
Content-Type: text/plain; charset=UTF-8; 
    name*0="=?UTF-8?Q?=ED=95=9C=EA=B8=80testfile=ED=95=9C=EA=B8=80?=
 =?U"; 
    name*1="TF-8?Q?=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=8"; 
    name*2="0testfil?=
 =?UTF-8?Q?e=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80=E"; 
    name*3="D=95=9C?= =?UTF-8?Q?=EA=B8=80=ED=95=9C=EA=B8=80tes?=
 =?UTF-8"; 
    name*4="?Q?tfile=ED=95=9C?=
 =?UTF-8?Q?=EA=B8=80=ED=95=9C=EA=B8=80=ED"; 
    name*5="=95=9C=EA=B8=80=ED=95=9C=EA=B8=80?=
 =?UTF-8?Q?testfile=ED=95"; 
    name*6="=9C=EA=B8=80=ED=95=9C=EA=B8=80?=
 =?UTF-8?Q?=ED=95=9C=EA=B8=8"; 
    name*7="0=ED=95=9C=EA=B8=80testfile=ED=95=9C?=
 =?UTF-8?Q?=EA=B8=80=E"; 
    name*8="D=95=9C=EA=B8=80=ED=95=9C=EA=B8=80=ED=95=9C?=
 =?UTF-8?Q?=EA="; 
    name*9="B8=80testf?=
 =?UTF-8?Q?ile=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8="; 
    name*10="80=ED=95=9C=EA=B8=80.txt?="
Content-Transfer-Encoding: base64
Content-Disposition: attachment; 
    filename*0="=?UTF-8?Q?=ED=95=9C=EA=B8=80testfile=ED=95=9C=EA=B8=80?=
 =?U"; 
    filename*1="TF-8?Q?=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=8"; 
    filename*2="0testfil?=
 =?UTF-8?Q?e=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80=E"; 
    filename*3="D=95=9C?= =?UTF-8?Q?=EA=B8=80=ED=95=9C=EA=B8=80tes?=
 =?UTF-8"; 
    filename*4="?Q?tfile=ED=95=9C?=
 =?UTF-8?Q?=EA=B8=80=ED=95=9C=EA=B8=80=ED"; 
    filename*5="=95=9C=EA=B8=80=ED=95=9C=EA=B8=80?=
 =?UTF-8?Q?testfile=ED=95"; 
    filename*6="=9C=EA=B8=80=ED=95=9C=EA=B8=80?=
 =?UTF-8?Q?=ED=95=9C=EA=B8=8"; 
    filename*7="0=ED=95=9C=EA=B8=80testfile=ED=95=9C?=
 =?UTF-8?Q?=EA=B8=80=E"; 
    filename*8="D=95=9C=EA=B8=80=ED=95=9C=EA=B8=80=ED=95=9C?=
 =?UTF-8?Q?=EA="; 
    filename*9="B8=80testf?=
 =?UTF-8?Q?ile=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8="; 
    filename*10="80=ED=95=9C=EA=B8=80.txt?="
Content-Description: =?UTF-8?Q?=ED=95=9C=EA=B8=80testfile=ED=95=9C=EA=B8=80?=
 =?UTF-8?Q?=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80testfil?=
 =?UTF-8?Q?e=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80=ED=95=9C?=
 =?UTF-8?Q?=EA=B8=80=ED=95=9C=EA=B8=80tes?= =?UTF-8?Q?tfile=ED=95=9C?=
 =?UTF-8?Q?=EA=B8=80=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80?=
 =?UTF-8?Q?testfile=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80?=
 =?UTF-8?Q?=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80testfile=ED=95=9C?=
 =?UTF-8?Q?=EA=B8=80=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80=ED=95=9C?=
 =?UTF-8?Q?=EA=B8=80testf?=
 =?UTF-8?Q?ile=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80=ED=95=9C=EA=B8=80.txt?=

log using javax.mail-api.jar in debugging mode.
Content-Type: text/plain; charset=UTF-8; 
    name*=UTF-8''%ED%95%9C%EA%B8%80testfile%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80testfile%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80testfile%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80testfile%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80testfile%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80testfile%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80.txt
Content-Transfer-Encoding: base64
Content-Disposition: attachment; 
    filename*=UTF-8''%ED%95%9C%EA%B8%80testfile%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80testfile%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80testfile%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80testfile%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80testfile%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80testfile%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80%ED%95%9C%EA%B8%80.txt

Solution

  • This problem is solved thanks to @Bill Shannon


    • modify MultiPartEmail.java (commons-email.1.5.jar)
    // [before]
    Line 467 bodyPart.setFileName(MimeUtility.encodeText(name));
    
    // [after]
    // I would make an option to handle it like this.
    Line 467 bodyPart.setFileName(name);