gpt4 book ai didi

java - 附件名称编码失败

转载 作者:行者123 更新时间:2023-11-30 03:02:14 25 4
gpt4 key购买 nike

我尝试发送一封带有附件的电子邮件(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 但如果我有这个String f = name + "Πρόγραμμα Iανουάριος 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");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 FAQ 中所述。 。然后使用标准 RFC 2231 技术将该非标准编码文本拆分为多个参数。正是这种非标准和标准格式的混合可能导致邮件阅读器感到困惑。

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

关于java - 附件名称编码失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35673240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com