Change content type multipart / XXX without changing base parts

I have an instance of MimeMessage that contains encrypted parts.

Source content type "multipart / encrypted; protocol =" application / pgp-encrypted "; border =" EncryptedBoundary12312345654654 "

After decrypting each part, I want the multi-part header to change as:

"multipart/mixed; boundary="EncryptedBoundary12312345654654" 

The boundary number is obviously dynamic, then I can’t just do

 mime.setHeader("Content-Type", "multipart/mixed;" ); 

Do you have any idea of ​​best practice for this case?

+2
source share
2 answers

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.

+1
source

I don’t understand what you mean when you say that you "want the variable header to change." Are you trying to decrypt the message "in place"? Perhaps this will not work.

You can create a new message using the decrypted contents of the original message. If it is important for you that things like the “boundary” value remain the same, you probably need to subclass MimeMultipart and use the ContentType class to create a new content type value.

+2
source

Source: https://habr.com/ru/post/1444129/


All Articles