问题描述
我尝试发送带有附件的电子邮件(一个 pdf 文件),但接收者收到了一个名称不同且没有 .pdf 结尾的文件.该文件的名称是希腊语..
I try to send an email with an attachment (A pdf file), but the receiver receives a file with a different name and without the .pdf ending. The name of the file is in Greek..
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,但如果我有这个String f = name + " Πρόγραμμα Ιανουάριος 2016.pdf";
他没有.他越来越喜欢=_UTF-8_B_zpzOtc Dz4POsc67zrHPgiDOmc6xzr3Ov8 FzqzPgc65zr_Pgi___ ___filename_1=__5wZGY=_=
the name
is a string variable and is getting a value previously. The strange is that if I have String f = name + " αααα.pdf"
the receiver is getting a pdf succesfully with the name Ρουβάς αααα.pdf
but if i have this String f = name + " Πρόγραμμα Ιανουάριος 2016.pdf";
he doesn't. He is getting sth like=_UTF-8_B_zpzOtc Dz4POsc67zrHPgiDOmc6xzr3Ov8 FzqzPgc65zr_Pgi___ ___filename_1=__5wZGY=_=
我添加了 message.writeTo(System.out);
然后我得到了:
I added the message.writeTo(System.out);
and I got:
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");
或 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 常见问题解答.然后使用标准 RFC 2231 技术将非标准编码文本拆分为多个参数.正是这种非标准和标准格式的混合可能导致邮件阅读者感到困惑.
Because you're encoding the filename yourself, you're using the non-standard MIME encoding format, as described in the JavaMail FAQ. That non-standard encoded text is then being split into multiple parameters using the standard RFC 2231 technique. It's this mix of non-standard and standard format that's probably causing the confusion for the mail reader.
尝试通过删除对 MimeUtility.encodeText
的调用让 JavaMail 为您进行编码.如果这不起作用,请将系统属性 mail.mime.encodeparameters
设置为 false
以禁用 RFC 2231 编码.
Try letting JavaMail do the encoding for you by removing the call to MimeUtility.encodeText
. If that doesn't work, set the System property mail.mime.encodeparameters
to false
to disable the RFC 2231 encoding.
这篇关于附件的名称编码失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!