java - 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);
有没有人有同样的问题?
谢谢你的帮助。
解决方案
我找到了一个我无法理解的解释。事实上,文件名是“正常”发送的,但在标题中使用格式
Content-Disposition : attachment; filename*=Cp1252''newName.pdf .
如果我不设置文件名,我有:
Content-Disposition : attachment; filename=oldName.pdf
(我需要修改名称,所以我使用了 fileName 设置器)。我无法解释为什么,但“* = Cp1252''”部分是附件中缺少名称的原因,但只有在邮件签名之后(如果没有,我有好名字)。所以我的解决方案是直接通过以下方式修改名称:
attachmentPart.setHeader("Content-Disposition", "newName.pdf")
它有效!它并不优雅,但它是一个解决方案。
推荐阅读
- javascript - openTok:publisher.setVideoSource(deviceId) return 无法在“MediaStream”上执行“addTrack”:参数 1 不是“MediaStreamTrack”类型
- mysql - Kubernetes - 如何为我的有状态数据库使用状态集?
- azure-devops - 基于目录数量的动态作业
- javascript - 如何使用新的 Webpack devServer 配置?
- python - Python - getline() 方法不打印“换行符”
- postgresql - 导入 SQL 文件:PostgreSQL
- swift - 关于 Observable 的条件句的问题。(RxSwift)
- convex-optimization - 使用 LMI 约束执行凸优化
- java - java 17 特征向量对比。Java Util Vector,相同还是不同?
- javascript - 将数据数组转换为 SectionList 组件的分组数据