1 Reply Latest reply: Oct 10, 2011 5:48 PM by Bill Shannon-Oracle RSS

    Modify existing Message

    893137
      Hi all,

      I need to change text/html part of message which I have in mail format. How to do this easily without loosing other parts of message?
      Or maybe you know some better way to create kind of simple proxy for SMTP which would add and replace some text in HTML part of email?

      So far I am reading message from plain stream of mail text representation (got it from Subethamail SMTP server) into JavaMail Message object. I had problem, when my changes to message were not reflected - I posted question on http://stackoverflow.com/questions/7674775/how-to-modify-existing-java-mail-mimemessage-body-parts/ and solved this one by calling message.saveChanges();

      The issue is that when I call message.saveChanges() headers seams to be lost - especially those around Content Type and Charset.

      My code looks like this:
      public void resend(InputStream data) throws Exception {
      
      ...
      
              Transport transport = mailSession.getTransport();
              MimeMessage message = new MimeMessage(mailSession, data);
      
              Object content = message.getContent();
              if (MimeMultipart.class.isAssignableFrom(content.getClass())) {
                  MimeMultipart mimeMultipart = (MimeMultipart) content;
      
                  for (int i = 0; i < mimeMultipart.getCount(); i++) {
      
                      MimeBodyPart bodyPart = (MimeBodyPart) 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);
      
                          String contentType = bodyPart.getContentType();
                          System.out.println("Updating content type to = " + contentType);
                          bodyPart.setContent(cnt, contentType);
                      } else if (bodyPart.getContentType().startsWith("text/html")) {
                          String cnt = updateContent((String) bodyPart.getContent());
                          System.out.println("ContentType = " + bodyPart.getContentType());
                          System.out.println("Content = " + cnt);
      
                          String contentType = bodyPart.getContentType();
                          System.out.println("Updating content type to = " + contentType);
                          bodyPart.setContent(cnt, contentType);
                      }
                  }
              } else {
                  String cnt = updateContent((String) message.getContent());
                  System.out.println("ContentType = " + message.getContentType());
                  System.out.println("Content = " + cnt);
      
                  String contentType = message.getContentType();
                  System.out.println("Updating content type to = " + contentType);
                  message.setContent(cnt, contentType);
              }
      
              message.saveChanges();
      
      ...
      
      }
      Mark on Stackoverflow suggested that problem is in bodyPart.setContent(cnt, contentType); call...

      Hope that makes sense for you... and you will be able to help me...

      Thanks in advance
      Konrad

      Edited by: 890134 on 2011-10-07 08:29
        • 1. Re: Modify existing Message
          Bill Shannon-Oracle
          JavaMail wasn't really design to support "editing" of existing messages.
          That's caused some problems over the years and I've had to fix a number
          of bugs in this area. The current version (1.4.4) definitely works better
          than older versions, but I won't be surprised if there are still bugs.

          Even so, your code is making a number of invalid assumptions about the
          structure of messages. I would suggest looking at the msgshow.java
          demo program. It includes code that visits all the parts of a message.
          It should be straightforward to extend that code to detect the parts you
          want to modify and to change the content of those parts using the
          setContent (or setText) method.

          If it's still not working for you, provide more detail about what you're doing,
          including a same message. (Send the details to me at javamail_ww@oracle.com
          if you prefer.)