2014-01-16 53 views
0

我試圖在我的Java程序中使用Apache Commons發送HTML電子郵件。這是我的代碼:將電子郵件發送到以下服務器失敗(apache commons - Java)

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("[email protected]", "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一個問題...當運行罐子的「解包」的版本(我在一個罐子裏的代碼, apache/javax在一個獨立的jar文件夾中),一切工作正常。當運行'組合分發'(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

您的服務器可以連接到'mail.axc.nl'嗎?有防火牆嗎? –

+0

也許在stacktrace中有更多有意義的信息(比如Connection Refused)(特別是在最低的異常中)? –

+0

出於某種原因,它似乎根本不能從編譯的jar文件中運行。它在我的桌面和我的服務器上得到異常。但是當我在netbeans中運行它時,一切正常。我在一分鐘內添加堆棧跟蹤。 –

回答

1

感謝傑拉德施耐德,我找到了解決辦法。

在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); 
相關問題