简体   繁体   中英

Sending the email to the following server failed (apache commons - Java)

I'm trying to send an HTML email using Apache Commons in my Java program. This is my code:

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();

Now, this works like a charm on my desktop computer (running Windows 8). Now, on my server (running Windows Server 2012), it fails. It always gets the following exception:

Sending the email to the following server failed: mail.axc.nl:465

I checked my credentials a dozen times, nothing wrong with them. I can't figure out why this wouldn't work on my Server, while it works perfectly fine on my desktop :S

UPDATE: seems like the error occurs whenever you compile and run the program. When my code runs in netbeans, it works just fine. Here's the stack trace:

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

UPDATE 2: Seems like there might be a problem in my build.xml... When running the 'unpacked' version of the jar (my code in a jar, and apache/javax in a seperate jar's in a lib folder), everything works fine. When running the 'combined distribution' (apache, javax and my code packed in one jar), it goes wrong.

Anyone spots an error in my build.xml? I've been using it for quite some time now, never had any problems :/

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

Thanks to Gerald Schneider, I found a solution.

Googling a little further on the DCH exception, I found that adding this code before creating an Email object fixes it!

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);

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