【问题标题】:Why attachment file name broken when using the apache-email library?为什么使用 apache-email 库时附件文件名损坏?
【发布时间】:2019-12-14 18:21:00
【问题描述】:

邮件是使用 apache-email 库发送的。(commons-email-1.5.jar) 如果附件文件名中包含韩语,则附件文件名会损坏。正常发送文件名的方式是什么? 不要直接使用 javax.mail-api 库。只应使用 apache-email 库。


试试这个

  • 设置处置(附件,内联)
  • 设置编码(utf-8、euc-kr)
  • 使用 javax.mail-api 库(它工作正常,但不应使用。)

源代码

使用 commons-email-1.5.jar (附件文件名损坏)
// 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();
使用 javax.mail-api.jar (附件文件名不坏)
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();
}

实际结果

使用 commons-email-1.5.jar - 文件名损坏。
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_=
使用 javax.mail-api.jar - 文件名正常
attachment file name : 한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글한글testfile한글한글한글.txt

邮件发送日志

在调试模式下使用 commons-email-1.5.jar 记录
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?=

在调试模式下使用 javax.mail-api.jar 记录。
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

【问题讨论】:

  • commons-email-1.5 使用的是什么版本的 JavaMail?也许它使用的是不处理非 ASCII 文件名的旧版本?
  • JavaMail 版本为 1.6.1。 (javax.mail-1.6.1.jar)。另外,在测试版本升级到1.6.2时也得到了同样的结果。
  • 我尝试了另一种方法,当我更改 MultiPartEmail.java 的源时,它工作正常。更详细地说,[MultiPartEmail.java 第 467 行]bodyPart.setFileName(MimeUtility.encodeText(name)) --> bodyPart.setFileName(name)。我更改了 MultiPartEmail.java 并得到了与 javax.mail-api.jar 相同的
  • 我修改了MultiPartEmail.java,得到的结果和javax.mail-api.jar一样。这是正确的吗?
  • 是的,commons-email 不应该为您编码文件名;它应该让 JavaMail 做到这一点。

标签: java email smtp jakarta-mail apache-commons-email


【解决方案1】:
感谢@Bill Shannon 解决了这个问题


  • 修改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); 

【讨论】:

    猜你喜欢
    • 2014-02-11
    • 2015-05-24
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    相关资源
    最近更新 更多