I am using javamail, through Jodd Mail, to load e-mail messages from IMAP inboxes. The application is deployed in glassfish and running without problems in Windows with Oracle Java 7.
Running in glassfish in a CentOS 64bit machine, with Oracle Java 7, there is a problem. When loading an image attachment an exception is thrown:
java.lang.IllegalArgumentException: failed to parse:
at java.awt.datatransfer.DataFlavor.<init>(DataFlavor.java:368)
at javax.activation.ActivationDataFlavor.<init>(ActivationDataFlavor.java:81)
at com.sun.xml.internal.ws.encoding.ImageDataContentHandler.<init>(ImageDataContentHandler.java:57)
at sun.reflect.GeneratedConstructorAccessor3931.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.Class.newInstance(Class.java:379)
at javax.activation.MailcapCommandMap.getDataContentHandler(MailcapCommandMap.java:588)
at javax.activation.MailcapCommandMap.createDataContentHandler(MailcapCommandMap.java:542)
at javax.activation.CommandMap.createDataContentHandler(CommandMap.java:221)
at javax.activation.DataHandler.getDataContentHandler(DataHandler.java:614)
at javax.activation.DataHandler.getContent(DataHandler.java:542)
at javax.mail.internet.MimeBodyPart.getContent(MimeBodyPart.java:657)
at jodd.mail.ReceivedEmail.processPart(ReceivedEmail.java:89)
at jodd.mail.ReceivedEmail.processPart(ReceivedEmail.java:124)
at jodd.mail.ReceivedEmail.parseMessage(ReceivedEmail.java:82)
The attachment has the following headers:
Content-Type: image/png;
name="image1.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="image1.png"
I tried running the same code snippet in a 32bit linux machine with the Oracle Java 7 and it ran without problems.
Is this a bug in javamail/java activation?
There's a bug in the interaction between JavaMail and JAX-WS. We put a fix into the JDK recently to address this problem. I believe it's available in the latest version, so you might need to update your JDK on your CentOS machine.
Also, I believe this problem is caused by calling the getContent method on an image. Many people write code that calls getContent and then uses the returned Java type to determine what to do with the content. This is very inefficient, and causes you to create Java objects that you many never need. Instead, you should use the isMimeType method to decide what to do with the content based on its MIME type. If all you're going to do with an image attachment is save it to a file (for example), you never need to call getContent. That will avoid running into the bug you're seeing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With