从java5和java6发送邮件 [英] send a mail from java5 and java6

查看:136
本文介绍了从java5和java6发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道类似的问题已被问过许多次,但我认为这个问题有些不同:)我正在写一个maven报告插件,它会发送电子邮件到用户列表。我现在有这个问题,当我使用java5运行代码时,代码似乎工作正常,但与java6一起失败。
实际上,插件是在Groovy中编写的,并使用commons-email实用程序发送html消息:

  HtmlEmail电子邮件= 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:激活:1.1.1。



如果我使用我的新插件运行Maven项目,我得到java6的这个异常:

  javax.activation.UnsupportedDataTypeException:无对象DCH for MIME类型multipart / mixed; 
boundary =---- = _ Part_0_11139111.1262007863993在com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)上的
在javax.mail.Transport处
。 send0(Transport.java:189)

使用java5我没有任何问题。



我尝试了以下解决方法:


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

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

    这也不适用于java6,但它确实表明请求的mimetype未在mailcap中注册(请参见使用'println'日志统计的循环)。

     原始MIME类型:image / jpeg 
    原始MIME类型:image / gif
    原始MIME -TYPE:text / *
    替换的MIME-TYPE:message / rfc822
    替换的MIME-TYPE:multipart / *
    替换的MIME-TYPE:text / plain
    替换的MIME-TYPE :text / xml
    替换的MIME类型:multipart / mixed
    替换的MIME类型:text / html
    替换的MIME类型:image / jpeg
    替换的MIME类型:image / gif
    替换的MIME类型:text / *
    替换的COMMAND:javax.activation.CommandInfo@1e5d007
    替换的COMMAND:javax.activation.CommandInfo@bc8f01
  2. href =http://java.sun.com/j2ee/1.4/docs/api/javax/activation/MailcapCommandMap.html =nofollow noreferrer> http://java.sun.com/j2ee/1.4/文档/ API /的javax /激活/ MailcapCommandMap.html )。所以我的问题是,是否有人有任何想法,我怎么看我的得到在java5和java6上工作的代码/配置:)

    解决方案

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



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



    引用(页面结尾): http://old.nabble.com/javax.activation.UnsupportedDataTypeException:-no-object-DCH-for-MIME-type-多部分混合td12523671.html

    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 :)

    解决方案

    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.

    这篇关于从java5和java6发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆