簡體   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

我使用以下代碼通過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;
}

但是我得到了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

經過一些搜索,我發現有人有同樣的問題,但沒有正確的答案(我依舊添加以下代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM