从java5和java6发送邮件

发布于 2024-08-16 08:43:25 字数 2802 浏览 4 评论 0原文

我知道类似的问题以前已经被问过很多次了,但我认为这个有点不同:)

我正在编写一个 Maven 报告插件,它将向用户列表发送电子邮件。我现在遇到的问题是,当我使用 java5 运行代码时,代码似乎工作正常,但使用 java6 运行时则失败。 实际上,该插件是用 Groovy 编写的,并使用 commons-email 实用程序发送 html 消息:

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();

该项目依赖于 javax.mail:mail:1.4.1 和 javax.activation:activation:1.1.1。

如果我使用新插件运行 Maven 项目,我会在 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)

使用 java5 时我没有任何问题。

我尝试了以下解决方法:

  1. 以编程方式添加 mailcap 配置:

    // 添加主要邮件 MIME 类型的处理程序
    MailcapCommandMap mc = (MailcapCommandMap)CommandMap.getDefaultCommandMap();
    mc.getMimeTypes().each{ println "原始 MIME-TYPE: $it" }
    mc.getAllCommands("multipart/mixed").each { println "原始命令:$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 "替换的 MIME-TYPE: $it" }
    mc2.getAllCommands("multipart/mixed").each { println "替换的命令:$it" }
    

    这也不适用于 java6,但它确实表明请求的 mimetype 未在 mailcap 中注册(请参阅带有“println”日志语句的循环)。

    原始 MIME-TYPE:image/jpeg
    原始 MIME 类型:image/gif
    原始 MIME-TYPE:text/*
    替换的 MIME-TYPE:message/rfc822
    替换的 MIME-TYPE:multipart/*
    替换的 MIME-TYPE:文本/纯文本
    替换的 MIME-TYPE:text/xml
    替换的 MIME-TYPE:多部分/混合
    替换的 MIME-TYPE:text/html
    替换的 MIME-TYPE:image/jpeg
    替换的 MIME-TYPE:image/gif
    替换的 MIME-TYPE:text/*
    替换命令:javax.activation.CommandInfo@1e5d007
    替换命令:javax.activation.CommandInfo@bc8f01
    
  2. 我创建了一个名为“mailcap”的文件并将其放置在插件的“META-INF”目录中(请参阅http://java.sun.com/j2ee/1.4/docs/api/javax/activation/MailcapCommandMap.html)。但这根本没有被拾取。

所以我的问题是,是否有人知道如何让代码/配置在 java5 和 java6 上工作:)

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:

  1. 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
    
  2. 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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生再见 2024-08-23 08:43:25

首先检查以确保没有 mail.jar、smtp.jar(旧)或activation.jar 的其他副本。 (最后一种可能性最大,因为您可能捆绑了activation.jar,因为它未包含在 JDK 1.5 中)。

如果这不起作用,或者由于运行插件的环境而无法控制,请尝试在创建邮件实例之前将上下文类加载器显式设置为系统类加载器。

参考(页尾): http://old.nabble.com/javax.activation.UnsupportedDataTypeException:-no-object-DCH-for-MIME-type-multipart-mixed-td12523671.html

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.

私野 2024-08-23 08:43:25

现在也可以通过将 java 升级到最新的 1.6.0.x 来修复此问题。
我不确定这个问题是什么时候解决的,但是从 x=6 到 x=27 为我解决了这个问题。

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.

烟沫凡尘 2024-08-23 08:43:25

对于那些在 Web 应用程序中工作并遇到此错误的人,将其放入startupservlet 的 service() 中可以解决该问题。

Thread.currentThread().setContextClassLoader( getClass().getClassLoader() ) 。谢谢马克。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文