JavaMailFun

From EggeWiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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(SMTPTransport.java:625)
        at javax.mail.Transport.send0(Transport.java:169)
        at javax.mail.Transport.send(Transport.java:98)

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");
        CommandMap.setDefaultCommandMap(mc);

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