简体   繁体   English

得到org.apache.commons.mail.EmailException使用common-mail通过Hotmail发送电子邮件

[英]Got org.apache.commons.mail.EmailException at using common-mail to send e-mail by Hotmail

I uses following code to send e-mail by hotmail service just like the User Guide 我使用以下代码通过hotmail服务发送电子邮件,就像用户指南一样

@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;
}

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

The details is 细节是

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

After some search,I found someone have the same problem,but not with correct answers(I reied to add follow code 经过一些搜索,我发现有人有同样的问题,但没有正确的答案(我依旧添加以下代码

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

But not working).I really don't know how to fix this 但没有用)。我真的不知道该如何解决

This exception is almost always a due to a problem in the environment in which your application runs. 这种异常几乎总是由于应用程序运行环境中的问题引起的。 Normally, JavaMail will use JAF to find the DataContentHandler for each type of data in your message. 通常,JavaMail将使用JAF为消息中的每种类型的数据查找DataContentHandler。 JAF reads a configuration file in the JavaMail mail.jar file. JAF读取JavaMail mail.jar文件中的配置文件。 If there's a problem with your class loaders, JAF might not be able to read that configuration file. 如果您的类加载器有问题,则JAF可能无法读取该配置文件。

A workaround that sometimes helps is: 有时有用的解决方法是:

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

in your main application class. 在您的主应用程序类中。

You can include the latest mail.jar and activation.jar in $CATALINA_HOME/lib folder and restart tomcat. 您可以在$CATALINA_HOME/lib文件夹中包含最新的mail.jaractivation.jar并重新启动tomcat。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM