簡體   English   中英

為什么使用apache-email庫時附件文件名損壞?

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

郵件是使用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 
@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); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM