问题
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 don't have any problems.
I tried the following workarounds:
Add the mailcap config programmatically:
// 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 really shows that the requested mimetype is not registered 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
I created a file called 'mailcap' and placed 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 question is, whether someone has any idea on how I get the code/configuration working on java5 and java6 :)
回答1:
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:-no-object-DCH-for-MIME-type-multipart-mixed-td12523671.html.
回答2:
This can now also be fixed by upgrading java to the latest 1.6.0.x. I'm not sure when this was fixed, but going from x=6 to x=27 fixed this for me.
回答3:
For those working in Web applications and getting this error, putting this in your startupservlet's service() fixes the issue.
Thread.currentThread().setContextClassLoader( getClass().getClassLoader() ) . Thanks Marc.
来源:https://stackoverflow.com/questions/1969667/send-a-mail-from-java5-and-java6