gpt4 book ai didi

java - 似乎 JavaMail 的 MimeBodyPart.setFileName 在电子邮件中插入换行符并导致文件名显示为无效

转载 作者:行者123 更新时间:2023-11-30 01:59:31 25 4
gpt4 key购买 nike

我们有代码可以发送到一个人的邮箱并将带有附件的电子邮件复制到文件系统。

复制消息和附件的代码对于大多数文件都可以正常工作,但长文件名会出现问题。

if (attachment instanceof FileAttachment || attachment.getIsInline()) {
System.out.println(attachment.getName());
String FILE_NAME = "C:path\\" + attachment.getName();
attachment.load(FILE_NAME);

MimeBodyPart attachmentMime = new MimeBodyPart();
attachmentMime.setContent(new MimeMultipart(attachment.getContentType()));
javax.activation.DataSource source = new FileDataSource(FILE_NAME);
attachmentMime.setDataHandler(new DataHandler(source));
attachmentMime.setFileName(attachment.getName());
multipart.addBodyPart(attachmentMime);
}

例如,文件名:“CRLM 群体的 SKI17042 手术 CPT 选择的副本.xlsx”显示在名称为“无标题附件 00006.dat”的电子邮件附件中。当我查看创建的 .eml 文件时,看起来 JavaMail 在文件名中间插入了一个换行符,这可能会导致问题。

当我在文本编辑器中打开 .eml 时,我看到带有换行符的标题(请注意跨越文件名的第 3/4 行和 7/8 行中的换行符:

------=_Part_3_840180718.1542390637623
Content-Type: application/octet-stream;
name*0="Copy of SKI17042 surgery CPT choices for CRLM
population.xls"; name*1=x
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename*0="Copy of SKI17042 surgery CPT choices for CRLM
population.xls"; filename*1=x

文件内容很好,如果您将文件放在 .xlsx 上,它会在 Excel 中打开,内容符合预期。

有人对如何解决 JavaMail 中文件附件名称的问题有任何信息或想法吗?

谢谢!

编辑 - 解决方案

    Properties props = System.getProperties();

props.put("mail.mime.splitlongparameters", false);

Session session = Session.getInstance(props, null);

createProjectFolder(folder);

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(objectJSON.getString("from"), objectJSON.getString("fromName")));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(objectJSON.getString("to")));
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(objectJSON.getString("cc")));
message.setSubject(objectJSON.getString("subject"));
message.setSentDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a").parse(objectJSON.getString("date")));

// create the message part
Multipart multipart = new MimeMultipart("mixed");
MimeBodyPart content = new MimeBodyPart();

// fill message
if (objectJSON.getString("body").toLowerCase().contains("html")) {
content.setContent( objectJSON.getString("body"), "text/html; charset=utf-8" );
}
else {
content.setText(objectJSON.getString("body"), "utf-8");
}

multipart.addBodyPart(content);

if (objectJSON.getInt("hasAttachment") == 1) {

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);

service.setUrl(new URI("https://mail/ews/Exchange.asmx"));

ExchangeCredentials credentials = new WebCredentials(developerEmail, password);

service.setCredentials(credentials);

try {

EmailMessage messageWithAttachment = EmailMessage.bind(service, new ItemId(emailId));

AttachmentCollection attachmentsCol = messageWithAttachment.getAttachments();
System.out.println("attachments: " + attachmentsCol.getCount());
for (int i = 0; i < attachmentsCol.getCount(); i++) {
FileAttachment attachment = (FileAttachment)attachmentsCol.getPropertyAtIndex(i);

if (attachment instanceof FileAttachment || attachment.getIsInline()) {
System.out.println(attachment.getName());
String FILE_NAME = "C:\\R2D4\\eclipse-workspace\\DataLine\\WebContent\\WEB-INF\\email_attachments\\" + attachment.getName();
attachment.load(FILE_NAME);

MimeBodyPart attachmentMime = new MimeBodyPart();
attachmentMime.setContent(new MimeMultipart(attachment.getContentType()));
javax.activation.DataSource source = new FileDataSource(FILE_NAME);
attachmentMime.setDataHandler(new DataHandler(source));
attachmentMime.setFileName(attachment.getName());
multipart.addBodyPart(attachmentMime);

}

}

}
catch(Exception e) {
e.printStackTrace();
}

service.close();

}


// integration
message.setContent(multipart);
message.saveChanges();

最佳答案

如果文件名长度超过 60 个字符,它将被拆分为多个参数,如 RFC 2231 中所述。 .

您处理消息的代码似乎不了解如何处理 RFC 2231 编码参数。以下是可能的解决方案:

  • 您可以通过设置属性mail.mime.encodeparameters来禁用所有RFC 2231编码的使用。为

  • 您可以通过将(不幸的是未记录的)属性 mail.mime.splitlongparameters 设置为 false 来禁用长参数的拆分。

关于java - 似乎 JavaMail 的 MimeBodyPart.setFileName 在电子邮件中插入换行符并导致文件名显示为无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53343113/

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