我正在尝试在我的Java程序中使用Apache Commons发送HTML电子邮件。这是我的代码:
HtmlEmail email = new HtmlEmail();
email.setHostName("mail.axc.nl");
email.setSslSmtpPort("465");
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);
email.addTo(emailadr, name);
email.setFrom("noreply@smartfix.me", "SmartFix");
email.setSubject("Subject");
email.setHtmlMsg("here goes my message");
email.send();
现在,这就像我桌面计算机上的一个魅力(运行Windows 8)。现在,在我的服务器上(运行Windows Server 2012),它失败了。它总是会出现以下异常:
Sending the email to the following server failed: mail.axc.nl:465
我检查了我的凭证十几次,没有错。我无法弄清楚为什么这对我的服务器不起作用,而它在我的桌面上运行得非常好:S
更新:似乎每当编译和运行程序时都会发生错误。当我的代码在netbeans中运行时,它工作得很好。这是堆栈跟踪:
C:\inetpub\tracking>java.exe -jar SmartFixGUI.jar
SmartFix Tracking Terminal - loaded successfully.
Currently tracking null
71QJD73I3I is a smartphone.
Currently tracking 71QJD73I3I
71QJD73I3I is a smartphone.
org.apache.commons.mail.EmailException: Sending the email to the following serve
r failed : mail.axc.nl:465
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410)
at org.apache.commons.mail.Email.send(Email.java:1437)
at smartfixgui.trackingNumber.sendMail(trackingNumber.java:378)
at smartfixgui.infoViewer.jButton2ActionPerformed(infoViewer.java:262)
at smartfixgui.infoViewer.access$200(infoViewer.java:19)
at smartfixgui.infoViewer$3.actionPerformed(infoViewer.java:149)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME ty
pe multipart/mixed;
boundary="----=_Part_0_1999183509.1389954279345"
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1182)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1400)
... 41 more
Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME
type multipart/mixed;
boundary="----=_Part_0_1999183509.1389954279345"
at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source)
at javax.activation.DataHandler.writeTo(Unknown Source)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1593)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1839)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1134)
... 44 more
更新2:似乎我的build.xml中可能存在问题...当运行jar的'unpacked'版本时(我的代码在jar中,而apache / javax在一个单独的jar中)一个lib文件夹),一切正常。当运行'组合发行版'(apache,javax和我的代码打包在一个jar中)时,它会出错。
有人在我的build.xml中发现错误吗?我已经使用它已经有一段时间了,从来没有遇到任何问题:/
<?xml version="1.0" encoding="UTF-8"?>
<project name="SmartFixGUI" default="default" basedir=".">
<description>Builds, tests, and runs the project SmartFixGUI.</description>
<import file="nbproject/build-impl.xml"/>
<target name="-post-jar">
<jar jarfile="dist/Combined-dist.jar">
<zipfileset src="${dist.jar}" excludes="META-INF/*" />
<zipfileset src="lib/commons-email-1.3.2.jar" excludes="META-INF/*" />
<zipfileset src="lib/javax.mail.jar" excludes="META-INF/*" />
<manifest>
<attribute name="Main-Class" value="smartfixgui.main"/>
</manifest>
</jar>
</target>
</project>
答案 0 :(得分:1)
感谢Gerald Schneider,我找到了解决方案。
在DCH异常上进一步搜索,我发现在创建电子邮件对象之前添加此代码会修复它!
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);