Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify existing Java mail MimeMessage body parts?

I am trying to modify existing MimeMessage body part. I would like to filter certain links. Does any of you know why even though body part content seams to be changed message is sent with old content? Is there some caching going on? 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");
}

Input stream "data" contains raw message.

Any ideas?

Thanks in advance....

like image 366
Konrad Pawlus Avatar asked Oct 06 '11 13:10

Konrad Pawlus


People also ask

What is MimeMultipart in Java?

The MimeMultipart class is an implementation of the abstract Multipart class that uses MIME conventions for the multipart data.

How do I get content from MimeMessage?

You can use Simple Java Mail (Open Source) to convert a MimeMessage to an Email object and then access everything, including attachments and embedded images, recipients, html, text and headers: Email email = EmailConverter. mimeMessageToEmail(mimeMessage);

What is MimeMessage in Java?

MimeMessage uses the InternetHeaders class to parse and store the top level RFC 822 headers of a message. The mail. mime. address. strict session property controls the parsing of address headers.


1 Answers

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

Updates the appropriate header fields of this message to be consistent with the message's contents. If this message is contained in a Folder, any changes made to this message are committed to the containing folder.

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

like image 173
Mark Rotteveel Avatar answered Oct 16 '22 17:10

Mark Rotteveel