首页 > 解决方案 > S/Mime 附件 java

问题描述

我尝试使用 S/Mime 协议签署电子邮件。它适用于身体。但是当我加入电子邮件的附件时,数字签名使电子邮件的名称(和扩展名)为空。因此,收到的电子邮件带有附件“whitoutTitle.dat”而不是“name.pdf”。我使用来自 BCMail 的 SMIMESignedGenerator。附件未修改。这就是为什么我很烦恼。

// This attachment part loose the name after the digital signature
File partFile = new File(fileName);
DataSource fds = new FileDataSource(partFile);
attachmentPart1.setDataHandler(new DataHandler(fds));
attachmentPart1.setFileName(partFile.getName());

// This attachment don't loose its name after the signature
attachmentPart2.setText(strinbBuilder.toString());
attachmentPart2.setFileName("name.txt");

multipart.addBodyPart(attachmentPart1);
multipart.addBodyPart(attachmentPart2);

mimemsg.setContent(multipart);

MimeBodyPart mimeBodyPart = mimemsg.getContent();

SMIMESignedGenerator generator = new SMIMESignedGenerator();
generator.addCertificates(getCertificateStore(smimeKey));
generator.addSignerInfoGenerator(getInfoGenerator(smimeKey));
MimeMultipart signedMimeMultipart = generator.generate(mimeBodyPart);
MimeBodyPart signedMimeBodyPart = new MimeBodyPart();
signedMimeBodyPart.setContent(signedMimeMultipart);

有没有人有同样的问题?

谢谢你的帮助。

标签: javasmime

解决方案


我找到了一个我无法理解的解释。事实上,文件名是“正常”发送的,但在标题中使用格式

Content-Disposition : attachment; filename*=Cp1252''newName.pdf . 

如果我不设置文件名,我有:

Content-Disposition : attachment; filename=oldName.pdf  

(我需要修改名称,所以我使用了 fileName 设置器)。我无法解释为什么,但“* = Cp1252''”部分是附件中缺少名称的原因,但只有在邮件签名之后(如果没有,我有好名字)。所以我的解决方案是直接通过以下方式修改名称:

attachmentPart.setHeader("Content-Disposition", "newName.pdf") 

它有效!它并不优雅,但它是一个解决方案。


推荐阅读