java - 在使用普通邮件发送电子邮件的Hotmail,得到org.apache.commons.mail.EmailException

  显示原文与译文双语对照的内容

我使用下面的代码通过hotmail服务发送 E-mail,就像用户指南一样。


@Override
public Boolean SendMsg(String title, String Content, String receiver, String sender) {
 try {
 HtmlEmail email = new HtmlEmail();
 final String username = s_address;
 final String password = s_pass;
 email.setHostName(s_smtp);
 email.setSmtpPort(Integer.parseInt(s_smtpp));
 email.setAuthentication(username, password);
 email.setSmtpPort(Integer.parseInt(s_smtpp));
 switch (c_smtps) {
 case '1':
 email.setSSL(true);
 email.setSslSmtpPort(s_smtpp);
 case '2':
 email.setTLS(true);
 }
 email.addTo(receiver);
 email.setFrom(s_address, sender);
 email.setSubject(title);
//set the html message
 email.setHtmlMsg(Content);

//set the alternative message
 email.setTextMsg(net.shisoft.util.common.html.Session.html2text(Content, true));
 email.send();
 return true;
 } catch (EmailException ex) {
 Logger.getLogger(ClassMail.class.getName()).log(Level.SEVERE, null, ex);
 }
 return false;
}

但我得到了 org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.live.com:587 异常。

细节是


org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.live.com:587
 at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)
 at org.apache.commons.mail.Email.send(Email.java:1267)
 at net.shisoft.rmi.server.svr.plugin.ClassMail.SendMsg(ClassMail.java:232)
 at net.shisoft.sdk.Logic.Mail.Send(Mail.java:108)
 at net.shisoft.communicates.ThreadActions.dealForward(ThreadActions.java:389)
 at net.shisoft.communicates.ThreadActions$1.run(ThreadActions.java:246)
Caused by: javax.mail.MessagingException: IOException while sending message;
 nested exception is:
 javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 
 boundary="----=_Part_3_499646563.1322072487744"
 at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:930)
 at javax.mail.Transport.send0(Transport.java:191)
 at javax.mail.Transport.send(Transport.java:120)
 at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
. . . 5 more
Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 
 boundary="----=_Part_3_499646563.1322072487744"
 at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:877)
 at javax.activation.DataHandler.writeTo(DataHandler.java:302)
 at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1383)
 at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1743)
 at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:888)
. . . 8 more

经过一些搜索,我发现有人有相同的问题,但是没有正确的答案( reied添加跟踪代码) 。


//add handlers for main MIME types
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);

但是没有工作我真的不知道如何修复

时间: 作者:

这里异常几乎总是由于应用程序运行环境中存在问题而导致的。 通常,JavaMail将使用JAF为消息中的每种数据类型查找 DataContentHandler 。 JAF在 JavaMail mail.jar 文件中读取一个配置文件。 如果类加载器有问题,JAF可能无法读取该配置文件。

有时帮助的解决办法是:


Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());

在你的主应用程序类中。

你可以在 $CATALINA_HOME/lib 文件夹中包含最新的mail.jaractivation.jar,并重启 tomcat 。

源:http://www.jguru.com/faq/view.jsp?EID=237257

作者:
...