2

メールは 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
4

1 に答える 1

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); 
于 2019-08-09T01:09:35.320 に答える