I am responding to post the code of my solution:
// 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();
Actually, there seems to be no other way to modify the original message, but making a copy was enough for me. The important point was the creation of a new MimeMultipart object, which will contain the decrypted parts, and then set as the contents of the MimeMessage (Wrapper). This will automatically create new content type values.
For information, we used MimeMessageWrapper, which is only a wrapper class that allows you to keep the message identifier unchanged (or not) in copies. One possible implementation is the Apache James project.
Another important point, finally, in this decision, the main parts were changed, but the border was adapted (it is not said that EncryptedXXXX is larger), which is even more clean for our case.
source share