views:

485

answers:

1

I know similar questions have been asked many times befor, but I think this one slitly different :)

I'm writing a maven report plugin which will send emails to a list of users. I now have the problem, that the code seems to be working fine when I run it with java5, but failes with java6. Actualy the plugin is writen in Groovy and uses the commons-email utilities to send a html message:

HtmlEmail email = new HtmlEmail();
email.setHostName(mailhost);
email.setSmtpPort(mailport);
email.setFrom(args.from);
email.addTo(args.receiver);
email.setSubject(args.subject);
email.setHtmlMsg(args.htmlmessage);
email.setDebug(log.isDebugEnabled());
email.send();

The project has dependencies to the javax.mail:mail:1.4.1 and the javax.activation:activation:1.1.1.

If I run a maven project using my new plugin, I'm getting this exception with java6:

    javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 
boundary="----=_Part_0_11139111.1262007863993"
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)
at javax.mail.Transport.send0(Transport.java:189)

With java5 I'dont have any problems...

I tried the following workarounds:

  1. Add the mailcap config programaticly:

    // add handlers for main mail MIME types
    MailcapCommandMap mc = (MailcapCommandMap)CommandMap.getDefaultCommandMap();
    mc.getMimeTypes().each{ println "Original MIME-TYPE: $it" }
    mc.getAllCommands ("multipart/mixed").each { println "Original COMMAND: $it" }
    
    
    mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
    mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
    mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
    mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
    mc.addMailcap("multipart/mixed;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
    

    mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); CommandMap.setDefaultCommandMap(mc);

    MailcapCommandMap mc2 = (MailcapCommandMap)CommandMap.getDefaultCommandMap();
    mc2.getMimeTypes().each{ println "Replaced MIME-TYPE: $it" }
    mc2.getAllCommands ("multipart/mixed").each { println "Replaced COMMAND: $it" }
    

    This also does not work with java6, but it realy shows that the requested mimetype is not registred in the mailcap (see loops with 'println' log statments).

    Original  MIME-TYPE: image/jpeg
    Original  MIME-TYPE: image/gif
    Original  MIME-TYPE: text/*
    Replaced MIME-TYPE: message/rfc822
    Replaced MIME-TYPE: multipart/*
    Replaced MIME-TYPE: text/plain
    Replaced MIME-TYPE: text/xml
    Replaced MIME-TYPE: multipart/mixed
    Replaced MIME-TYPE: text/html
    Replaced MIME-TYPE: image/jpeg
    Replaced MIME-TYPE: image/gif
    Replaced MIME-TYPE: text/*
    Replaced COMMAND: javax.activation.CommandInfo@1e5d007
    Replaced COMMAND: javax.activation.CommandInfo@bc8f01
    
  2. I created a file called 'mailcap' and place it in the 'META-INF' directory of the plugin (see http://java.sun.com/j2ee/1.4/docs/api/javax/activation/MailcapCommandMap.html). But this does not get picked up at all.

So my quite simple question is whether someone has an idea on how I get the code/configuration working on java5 and java6 :)

+1  A: 

First check to make sure there are no other copies of mail.jar, smtp.jar (old), or activation.jar. (The last is most likely, as you may have bundled activation.jar as it was not included in JDK 1.5).

If that does not work, or you can't control that due to the environment your plugin is being run from, try to explicitly set your context classloader to the system classloader prior to creating the mail instance.

Reference (end of page): http://old.nabble.com/javax.activation.UnsupportedDataTypeException%3A-no-object-DCH-for-MIME-type-multipart-mixed-td12523671.html.

Marc Paradise
a common case is to somehow have the geronimo implementation of javax.mail, which is buggy.
Bozho
Thanks for this! I actualy found the fullanswer at: http://blog.hpxn.net/2009/12/02/tomcat-java-6-and-javamail-cant-load-dch/ans was able to fix the problem by calling this: Thread.currentThread().setContextClassLoader( getClass().getClassLoader() )
domi
Ah, most excellent
Marc Paradise