From EggeWiki
Jump to navigation Jump to search

What could be simplier than sending an email from a command line app in Java? Well, a lot of things. I have an app which works fine in the IDE but fails via the command line with this exception:

javax.mail.MessagingException: IOException while sending message;
  nested exception is:
        javax.activation.UnsupportedDataTypeException: no object DCH for MIME type text/plain; charset=us-ascii
        at com.sun.mail.smtp.SMTPTransport.sendMessage(
        at javax.mail.Transport.send0(
        at javax.mail.Transport.send(

A quick Google search indicates that this is fairly common. In my case, the command line app runs via [OneJar], though most people have their fun when deploying to their app server. The first thing I tried was downloading activation.jar. I noticed in the IDE I have four different versions of the UnsupportedDataTypeException class. One comes from log4j, two from weblogic, and one from Jetty. I hoped adding a fifth via activation.jar would solve things. Well, no such luck.

Fortunately, I found one person's solution on [JGuru]. Austin Prichard-Levy suggests adding this to your mail script:

        MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
        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("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");

I did that, and my app started sending mail. I don't know how or why.