【问题标题】:Attachement's name encoding fails附件名称编码失败
【发布时间】:2016-06-10 22:32:12
【问题描述】:

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

try {
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress("from@mail.com"));
    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);
}

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="="

props.setProperty("mail.mime.encodeparameters", "false");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

【问题讨论】:

  • 您不需要明确编码文件名;当您调用 setFileName 时,JavaMail 应该为您执行此操作。您使用的是什么版本的 JavaMail?如果在 Transport.send 之前添加message.writeTo(System.out);,邮件标头是什么样的?正在使用什么程序来读取和显示收到的消息?
  • 我用JavaMail is 1.5.5.接收方拿到的文件是没有文件结尾的,所以如果你添加.pdf就可以正常打开了。
  • 我将message.writeTo(System.out); 的输出添加到问题@BillShannon
  • 如果你有 MimeMessageHelper 的实例,你可以添加 'mimeMessageHelper.setEncodeFilenames(false)' 会有所帮助

标签: java jakarta-mail mime-types mime


【解决方案1】:

因为您自己对文件名进行编码,所以您使用的是非标准 MIME 编码格式,如 JavaMail FAQ 中所述。然后使用标准 RFC 2231 技术将该非标准编码文本拆分为多个参数。正是这种非标准格式和标准格式的混合可能会导致邮件阅读器感到困惑。

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

【讨论】:

  • 我删除了 MimeUtility.encodeText 并在收到一个 pdf 文件时有所改进,但名称为 ????? ???? ???? ???.pdf 。然后我添加了:props.setProperty("mail.mime.encodeparameters", "false");true 但我也得到了 ????????.pdf
  • 我在问题中添加了message.writeTo(System.out);的结果
  • 您的默认字符集似乎不适用于文件名中的字符。尝试将系统属性mail.mime.charset 设置为“utf-8”。
  • @yaylitzis 属性mail.mime.encodeparameters是系统属性,所以你应该改写System.setProperty("mail.mime.encodeparameters", "false");
  • 嗨@BillShannon 我面临同样的问题,但在解码中。我的输入文件有 filename*0="Post Discharge Follow-Up Call Script doc$454c0369-1283-4446-a07d";有效载荷中的 filename*1=-cdc4cb175da8.pdf ,当我在下面进行解析时,它删除了解析对象中的 filename*1 。您能否建议解决方法。这就是我要解析的。 MimeMessage 消息=新 MimeMessage(IncomingMessage.getMessage()); MimeMessageParser 解析器 = new MimeMessageParser(message); parser.parse();当我这样做并尝试获取附件-> getfilename 时,它​​给出了空白文件名。
猜你喜欢
  • 2011-12-14
  • 1970-01-01
  • 2017-10-31
  • 2017-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-28
  • 2013-02-19
相关资源
最近更新 更多