简体   繁体   中英

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

@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.

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. JAF reads a configuration file in the JavaMail mail.jar file. If there's a problem with your class loaders, JAF might not be able to read that configuration file.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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