Attachement的名称编码失败

时间:2016-02-27 17:49:39

标签: java javamail mime-types mime

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

ViewPager

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

1 个答案:

答案 0 :(得分:2)

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

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