问题描述
我尝试发送带有附件的电子邮件(一个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,但是如果我有此字符串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);
,我得到了:
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
推荐答案
由于您要自行编码文件名,因此您使用的是非标准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.
这篇关于附件的名称编码失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!