我使用以下代码通过Hotmail服务发送电子邮件,就像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;
}
但我得到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);
但不工作。。我真的不知道如何解决这个问题
答案 0 :(得分:2)
此异常几乎总是由于您的环境中存在问题 应用程序运行通常,JavaMail将使用JAF来查找DataContentHandler 对于邮件中的每种数据类型。 JAF在中读取配置文件 JavaMail mail.jar文件。如果您的类加载器出现问题,JAF可能不会 能够读取该配置文件。
有时有用的解决方法是:
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
在主应用程序类中。
答案 1 :(得分:1)
您可以在mail.jar
文件夹中包含最新的activation.jar
和$CATALINA_HOME/lib
,然后重新启动tomcat。