我回答发布我的解决方案的代码:
// source is the encrypted MimeMessage
// MimeMessageWrapper is a wrapper which can copy a messgae but keep the message ID unchanged
boolean keepMessageId = true;
MimeMessageWrapper newMime = new MimeMessageWrapper(source, keepMessageId);
MimeMultipart mmp = new MimeMultipart("mixed");
List<MimePart> parts = MimeMultipartUtils.findPartsByMimeType(mime, "*");
for (MimePart part : parts) {
// Do some part processing
// Decrypt Adn verify individual parts
// End of processing
ContentType type = new ContentType(part.getContentType());
String encoding = part.getEncoding();
String name = type.getParameter("name");
part.setContent(new String(decPart.toByteArray()), type.toString());
// Add the part to the brand new MimeMultipart
mmp.addBodyPart((BodyPart) part);
}
// Set the original copy Message with the new modified content (decrypted parts)
mime.setContent(mmp);
mime.saveChanges();
事实上,似乎没有其他方法可以更改原始消息,但创建副本对我来说就足够了。重要的一点是创建一个新的 MimeMultipart 对象,该对象将包含解密的部分,然后作为内容提供给 MimeMessage(Wrapper)。这将“自动”生成新的内容类型值。
作为信息,我们确实使用了 MimeMessageWrapper,它只是一个包装类,可以使消息 ID 保持不变(或不变)到副本。一种可能的实现是在Apache James项目上。
另一个重要的一点,最后在那个解决方案中,底层部分被改变了,但边界也被调整了(不再说 EncryptedXXXX),这对我们的案例来说更加清晰。