Tomcat Java 6 and JavaMail: Can’t load DCH
Sending attachments with Java mail isn’t that difficult until you run into crazy weird error messages that don’t really tell you what the problem is. In this case DCH stands for Data Content Handler.
I was trying to set an input stream as an attachment to an e-mail message. This would all be running inside a Tomcat servlet container. In this case it was a PDF generated by iText. The error message I was receiving looked like:
Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; boundary="----=_Part_0_8898561.1259761862138" at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:877) at javax.activation.DataHandler.writeTo(DataHandler.java:302) at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350) at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1683) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:585) ... 30 more
This might or might not work depending on how you have your mailcap file set up but you might be able to see a debug message that shows you that the Java Activation Framework cannot load a class. Set the debug flag to true for the activation framework by setting a new Java system property:
After trying to send your email with an attachment you might see something like this appear before the stack trace:
Can't load DCH com.sun.mail.handlers.multipart_mixed; Exception: java.lang.ClassNotFoundException: com/sun/mail/handlers/multipart_mixed
Why is this happening?
I am not 100% sure of the technical reason why this is happening. I only know that this is due to a class loader that is not able to “see” the classes from com.sun.mail.handlers when running in the context of a servlet container.
When running the SAME code in a standalone application in ran perfectly but when in a context that uses classloaders differently (like tomcat or maybe a custom classloader environment) it will fail.
How do I fix it?
There are 3 solutions you can choose that best fits your situation.
1. Before you make the call to send the e-mail you can add:
Thread.currentThread().setContextClassLoader( getClass().getClassLoader() )
2. You can add the mail.jar (the jar that contains the data handlers we are unable to find) into the boot classpath like so:
3. You can place the activation.jar and mail.jar libraries in an endorsed folder which sets the libraries with a high precedence. You can specify your endorsed folder as a system property like so:
More information on this problem can be found here: