How to modify existing body parts of Java MimeMessage Java?

I am trying to modify an existing body part of a MimeMessage. I would like to filter out specific links. Do any of you know why, despite the fact that messages about the contents of the body part that need to be changed are sent with old content? Does caching continue? Any idea how to solve this?

Here is my code:

public void resend(InputStream data) throws Exception { Session mailSession = createMailSession(); //mailSession.setDebug(true); Transport transport = mailSession.getTransport(); MimeMessage message = new MimeMessage(mailSession, data); Object content = message.getContent(); if (content.getClass().isAssignableFrom(MimeMultipart.class)) { MimeMultipart mimeMultipart = (MimeMultipart) content; for (int i = 0; i < mimeMultipart.getCount(); i++) { BodyPart bodyPart = mimeMultipart.getBodyPart(i); if (bodyPart.getContentType().startsWith("text/plain")) { String cnt = updateContent((String) bodyPart.getContent()); System.out.println("ContentType = " + bodyPart.getContentType()); System.out.println("Content = " + cnt); bodyPart.setContent(cnt, bodyPart.getContentType()); } else if (bodyPart.getContentType().startsWith("text/html")) { String cnt = updateContent((String) bodyPart.getContent()); System.out.println("ContentType = " + bodyPart.getContentType()); System.out.println("Content = " + cnt); bodyPart.setContent(cnt, bodyPart.getContentType()); } } } else { String cnt = updateContent((String) message.getContent()); System.out.println("ContentType = " + message.getContentType()); System.out.println("Content = " + cnt); message.setContent(cnt, message.getContentType()); } transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } private String updateContent(String cnt) { return cnt.replace("www.xyz.pl", "www.new-xyz.pl"); } 

"Data stream data" contains an unhandled message.

Any ideas?

Thanks in advance....

+6
source share
1 answer

You need to call saveChanges () in MimeMessage (which as far as I know should be sufficient), see also: api-doc MimeMessage # saveChanges () :

Updates the corresponding header fields of this message to match the contents of the message. If this message is contained in a folder, any changes made to this message are transferred to the containing folder.

If any part of the message headers or contents is changed, saveChanges must be called to ensure that these changes are permanent. Otherwise, any such changes may or may not be saved, depending on the implementation of the folder.

+6
source

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


All Articles