Announcement Announcement Module
Collapse
No announcement yet.
MimeMessageHelper: Problems with alternative parts Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MimeMessageHelper: Problems with alternative parts

    Since the upgrade to latest Spring 1.2.3, I'm having problems with the MimeMessageHelper. Alternative text and html parts are not displayed correctly in MS Outlook. Outlook shows an empty email with the html multipart as a regular file attachment. Once you open the attachment, you can see the html formatted email (that you should see instantly, of course).

    The Changelog states that the multipart handling has changed in order to support Lotus Notes. The root item is now "multipart/mixed" with a "multipart/related" subpart containing the "multipart/alternative" and all inline attachments. Previously, the root part was "multipart/related" which was interpreted correctly by Outlook.

    I wonder if anyone else is having the same problem, or if I'm just doing something wrong. Otherwise, I'd suggest changing the behaviour back again, or at least to introduce an option that let's you choose what root item to use.

    Who is using Lotus Notes, anyway? ;-)

  • #2
    Me, or at least the organisation I work for.

    I posted the Jira issue that caused this change.

    There's hopefully some solution that works for both Lotus + Microsoft.

    A lot of big organisations use Lotus since it's not wide open to abuse/attacks like Outlook is. I would imagine that keeping things working in Lotus, even though it's old and crusty, would be a good thing for Spring since it means that it will work in more corporate environments.

    Can you post your code for building the mail message?

    I tried to go through the specification for mail messages, and it seems like there is some requirement as to how you actually build your message (might be wrong though?).

    Comment


    • #3
      It's surprisingly hard to make multipart mails work on multiple email clients... However, MimeMessageHelper's current strategy works nicely for me on Outlook 2003, including alternative text and HTML parts.

      Please post details on what doesn't work for you. Some minimal programmatic JavaMailSender code to reproduce the problem would be great: i.e. generating an email that doesn't work for you in Outlook.

      Juergen

      Comment


      • #4
        I'm happy to test it for Lotus if we can get some Outlook-happy code posted.

        Comment


        • #5
          So I assume Spring 1.2.1+ has been working fine on Lotus Notes then? As I noted, it also works for me on Outlook 2003. Of course, I've just tested a couple of simple use cases, so the devil might be in the details...

          Juergen

          Comment


          • #6
            Sorry for the late reply. I haven't been watching the forum during the last days.

            My code looks like this:

            Code:
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            String plainText = null;
            String htmlText = null;            
            
            // ... some other things happen here            
            
            plainText = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, realPath, encoding, model);
            htmlText = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, realPath, encoding, model);
                                    
            // create email text            
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, encoding);
            if ( null != plainText && null != htmlText && useAlternativeMultiParts ) {
                helper.setText(plainText, htmlText);                
            } else if ( null != htmlText ) {
                helper.setText(htmlText, true);
            } else {
               helper.setText(plainText);
            }
            As I said, it worked fine in the previous version of Spring. I have not tested the current behaviour with Outlook 2003, we are still using the 2000 version. I know that generating mails with alternative parts is always a pain. In another project we have been able to create mails that worked with most clients (including Lotus Notes and Outlook) using only the multipart/related type. Perhaps you can try if this could work here, too. The head of the MimeMessage looked like this:

            Code:
            From: [FROM]
            To: [TO]
            Subject: [SUBJECT]
            Date: [DATE]
            Content-Type: multipart/related; 
                type="multipart/alternative";
                boundary="---NEXT_PART_MAIL_001"
            MIME-Version: 1.0
            
            This is a multi-part message in MIME format.
            
            -----NEXT_PART_MAIL_001
            Content-Type: multipart/alternative;
                    boundary="---NEXT_PART_MAIL_002"
            
            
            -----NEXT_PART_MAIL_002
            Content-Type: text/plain;
                    charset="iso-8859-1"
            Content-Transfer-Encoding: 8bit
            If you want to have a copy of that mail, just drop me a note.

            Comment


            • #7
              Phantom Untitled Attachment

              I am seeing the same sort of problem. I played with it a little and I switched from using helper.getMimeMultipart.addBodyPart(htmlBodyPart); to using helper.getRootMimeMultipart.addBodyPart(htmlBodyPa rt); This resulted in the text for the email being shown in the email, but an attachment was still sent titled Untitled Attachment. The problem is that the customer has specifically requested that we not send attachments on our emails. With version 1.2 this worked fine, but with 1.2.1, 1.2.2, and 1.2.3 we get the phantom untitled attachment.

              Is there some sort of flag that has to be set in order for the attachment not to be sent?

              Comment


              • #8
                Untitled Attachment

                Never mind I got it. I needed to stop adding another body part to the message and instead manipulate the body part that is now being attached to the rootMimeMultipart. This works fine and we no longer have the attachment being sent. Thanks!

                Comment


                • #9
                  So to sum up, the remaining issue is: The nested multipart-mixed/related structure does work on Lotus Notes and Outlook 2003, but not on Outlook 2000 in some cases. While a plain multipart-related structure (as used by Spring until 1.2 final) does work on Outlook 2000 in all cases (and also on Outlook 2003), but not on Lotus Notes.

                  It would be great if someone could re-verify this. MimeMessageHelper's current behavior seems to work for me on both Outlook 2000 and 2003, and apparently it also works on Lotus Notes.

                  If we can't find a single way that works for all of those email clients in all cases, we'll need to make the mail structure configurable. However, it would be great if I we could double-check the mail client behavior first.

                  Juergen

                  Comment


                  • #10
                    FYI, I've introduced 3 multipart modes for MimeMessageHelper: MIXED, RELATED, and MIXED_RELATED. Overloaded constructors that such a constant as argument now, as alternative to the boolean multipart flag.

                    MIXED indicates a multipart message with a single root multipart element of type "mixed". Texts, inline elements and attachements will all get added to that root element. This corresponds to Spring 1.0's default behavior.

                    RELATED is analogous, but with a single root element of type "related". Texts, inline elements and attachements will all get added to that root element. This effectively corresponds to the default behavior of Spring 1.1 - 1.2 final.

                    MIXED_RELATED uses a root element of type "mixed" plus a nested element of type "related". Texts and inline elements will get added to the nested "related" element, while attachments will get added to the root "mixed" element. This is Spring 1.2.1+ default behavior.

                    Juergen

                    Comment


                    • #11
                      Phantom attachment

                      I could really use some help in the form of code here...
                      My emails have a phantom attachment as well and Im wondering what I did wrong
                      Heres my Code:

                      MimeMessage message = mailSender.createMimeMessage();

                      // use the true flag to indicate you need a multipart message
                      MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
                      helper.setFrom(fromAddress);
                      helper.setTo(toAddress);
                      helper.setText(body, true);
                      helper.setSubject(subject);
                      helper.addInline(domain, new File(FileUtil.FOLDER_STATIC + "/img/icon/small/favicon.gif"));

                      What am I doing wrong here?
                      Thanks for your help!

                      Comment

                      Working...
                      X