Search code examples
javaspringapache-cameljakarta-mail

JavaMail uses MimeMultipart from sun internal "saaj" package


I use Spring Boot and Apache Camel for a background process that reads emails, extracts an attachment and processes it.

In the first version this works fine. The second version adds a web service including sending an attachment. Now the email attachment extraction sometimes works and sometimes it does not. The debug message from Camel mailbinding class is:

No attachments to extract as content is not Multipart: com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart

I suspect that the saaj MimeMultipart class is used for web service + attachment but we have no idea why JavaMail chooses this class over javax.mail.internet.MimeMultipart?

Any idea? How can we prevent this?

Here is a the context of the above log line:

o.a.camel.component.mail.MailConsumer    : Fetching 1 messages. Total 1 messages.
o.a.camel.component.mail.MailConsumer    : Mail #0 is of type: com.sun.mail.imap.IMAPMessage - com.sun.mail.imap.IMAPMessage@28091f09
o.a.camel.component.mail.MailConsumer    : Mapping #0 from javax.mail.Message to Camel MailMessage
o.a.camel.component.mail.MailBinding     : Extracting attachments +++ start +++
o.a.camel.component.mail.MailBinding     : No attachments to extract as content is not Multipart: com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeMultipart
o.a.camel.component.mail.MailBinding     : Extracting attachments +++ done +++

Solution

  • This is a JRE bug: JDK-8043129 : JAF initialisation in SAAJ clashing with the one in javax.mail It says

    After initialisation of SAAJ components the JAF handlers defined defined by javax.mail don't work.

    It explains the successful processing of the first mail (which uses web services further down the processing pipeline) and failed attachment processing of subsequent mails.

    Resolution is easy: this bug was fixed in Java 7 Update 76 and Java 8 Update 20.

    This issue once again highlights the importance of keeping the same software versions in development and production environment.