附件名称编码失败

2024-05-12

我尝试发送一封带有附件的电子邮件(pdf 文件),但收件人收到的文件名称不同且没有 .pdf 结尾。该文件的名称是希腊语。

try {
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress("[email protected] /cdn-cgi/l/email-protection"));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mail));
    message.setSubject(title,"utf-8");

    // Create the message part
    BodyPart messageBodyPart = new MimeBodyPart();

    // Now set the actual message
    messageBodyPart.setText("This is message body");

    // Create a multipar message
    Multipart multipart = new MimeMultipart();

    // Set text message part
    multipart.addBodyPart(messageBodyPart);

    // Part two is attachment
    messageBodyPart = new MimeBodyPart();

    String filename = "file.pdf";
    String f = name + " Πρόγραμμα Ιανουάριος 2016.pdf";  // the desired name of the file
    DataSource source = new FileDataSource(filename);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(MimeUtility.encodeText(f, "UTF-8", null));
    multipart.addBodyPart(messageBodyPart);

    // Send the complete message parts
    message.setContent(multipart);

    Transport.send(message);

    System.out.println("Mail " + mail +" sent");
} catch (MessagingException e) {
    throw new RuntimeException(e);
}

the name是一个字符串变量并且之前正在获取一个值。奇怪的是,如果我有String f = name + " αααα.pdf"接收者成功获取带有名称的 pdfΡουβάς αααα.pdf但如果我有这个字符串f = name + " Πρόγραμμα Ιανουάριος 2016.pdf";他不知道。他正在变得像……=_UTF-8_B_zpzOtc Dz4POsc67zrHPgiDOmc6xzr3Ov8 FzqzPgc65zr_Pgi___ ___filename_1=__5wZGY=_=

我添加了message.writeTo(System.out);我得到了:

MIME-Version: 1.0
Content-Type: multipart/mixed; 
    bou

    ndary="----=_Part_0_1825884453.1457025565509"

    ------=_Part_0_1825884453.1457025565509
    Content-Type: text/plain; charset=us-ascii
    Content-Transfer-Encoding: 7bit

    This is message body
    ------=_Part_0_1825884453.1457025565509
    Content-Type: application/octet-stream; 
        name*0="=?UTF-8?B?zpzOtc+Dz4POsc67zrHPgiDOmc6xzr3Ov8+FzrHPgc6vzr/Pgi"; 
        name*1="Ay?=
     =?UTF-8?B?MDE2zpnOsc69zr/Phc6sz4HOuc6/z4IgMjAxNi5wZGY=?"; 
        name*2="="
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment; 
        filename*0="=?UTF-8?B?zpzOtc+Dz4POsc67zrHPgiDOmc6xzr3Ov8+FzrHPgc6vzr/Pgi"; 
        filename*1="Ay?=
     =?UTF-8?B?MDE2zpnOsc69zr/Phc6sz4HOuc6/z4IgMjAxNi5wZGY=?"; 
        filename*2="="

with props.setProperty("mail.mime.encodeparameters", "false"); or true

MIME-Version: 1.0
Content-Type: multipart/mixed; 
    boundary="----=_Part_0_797681969.1457074816557"

------=_Part_0_797681969.1457074816557
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is message body
------=_Part_0_797681969.1457074816557
Content-Type: application/octet-stream; name="?????????? 2016.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; 
    filename*=Cp1252''%3F%3F%3F%3F%3F%3F%3F%3F%3F%3F%202016.pdf

由于您自己对文件名进行编码,因此您使用的是非标准 MIME 编码格式,如JavaMail 常见问题解答 http://www.oracle.com/technetwork/java/javamail/faq/index.html#encodefilename。然后,使用标准 RFC 2231 技术将该非标准编码文本拆分为多个参数。正是这种非标准和标准格式的混合可能导致邮件阅读者感到困惑。

尝试通过删除对 JavaMail 的调用来让 JavaMail 为您进行编码MimeUtility.encodeText。如果这不起作用,请设置系统属性mail.mime.encodeparameters to false禁用 RFC 2231 编码。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

附件名称编码失败 的相关文章

随机推荐