Home > Java, Programming, Tomcat, Web Servers > Tomcat Java 6 and JavaMail: Can’t load DCH

Tomcat Java 6 and JavaMail: Can’t load DCH

December 2nd, 2009 Leave a comment Go to comments

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:

-Djavax.activation.debug=true

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:

-Xbootclasspath/p:mail.jar

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:

-Djava.endorsed.dirs="D:\apache-tomcat-6.0.20\endorsed"

More information on this problem can be found here:

http://72.5.124.102/thread.jspa?threadID=5174556&start=0&tstart=0


Categories: Java, Programming, Tomcat, Web Servers Tags:
  1. Yetish
    September 29th, 2010 at 14:16 | #1

    Thanks a Ton. I was struggling with this problem for over a week.

  2. Al
    July 18th, 2011 at 11:09 | #2

    Helped me out as well – thanks so much.

  3. Edu Castrillon
    August 30th, 2011 at 03:53 | #3

    Thaaaaaanks a ton !!

  4. Sujoy Acharya
    June 15th, 2012 at 09:20 | #4

    Hey you saved my life man!!! I was running a standalone java app as windows service using WinRun4j service API, when I ran the app from eclipse it worked fine but failed from WinRun4j when I added the line Thread.currentThread().setContextClassLoader( getClass().getClassLoader() ) it worked…. thanks man

  5. Nikita
    July 13th, 2012 at 08:24 | #5

    Thanks for your help… great work!

  6. hawk
    October 14th, 2012 at 11:05 | #6

    Thank you. I had same issue as Sujoy had, till found your post here. Life saver,,,,,,,!!!!

  7. maya
    October 16th, 2012 at 05:09 | #7

    Thanks big time!!

  8. nicola
    January 22nd, 2013 at 08:57 | #8

    Great post. Great work. Thanks!

  9. idelvane
    March 21st, 2013 at 16:00 | #9

    Thank you very much!

  10. Alec
    July 15th, 2013 at 16:58 | #10

    Thanks you just saved my program!

  11. Nit
    September 12th, 2013 at 05:00 | #11

    Thanks bro…helped a lot..We can also put these jars in tomcat/lib directory..

  1. No trackbacks yet.