为什么在使用 apache-email 库时附件文件名损坏?
Why attachment file name broken when using the apache-email library?
邮件是使用 apache-email 库发送的。(commons-email-1.5.jar)
如果附件文件名中包含韩语,则附件文件名已损坏。 filename通过什么方式可以正常发送?
不要直接使用 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
多亏 @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);
邮件是使用 apache-email 库发送的。(commons-email-1.5.jar) 如果附件文件名中包含韩语,则附件文件名已损坏。 filename通过什么方式可以正常发送? 不要直接使用 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
- 修改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);