Apache commons email always uses localhost for SMTP

1.8k views Asked by At

Why is when I setHostName("mail.metaregistrar.com") and sending the mail to use the SMTP on localhost?

Java code:

SimpleEmail email = new SimpleEmail();
            email.setHostName(host);
            email.setStartTLSEnabled(true);

            email.setSmtpPort(25);
            email.setAuthenticator(new DefaultAuthenticator(username, password));
            email.setFrom(from);
            email.setSubject(subject);
            email.setMsg(message);
            for (String address : addresses) {
                email.addTo(address);
            }
            email.send();  

Exception stack trace:

org.apache.commons.mail.EmailException: Sending the email to the following server failed : mail.metaregistrar.com:25
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1401)
    at org.apache.commons.mail.Email.send(Email.java:1428)
    at com.metaregistrar.mailer.Mailer.mail(Mailer.java:66)
    at com.metaregistrarv2.schedule.ExternalPollMessageIisseHandler.handleUpdateMessage(ExternalPollMessageIisseHandler.java:88)
    at com.metaregistrarv2.schedule.ExternalPollMessageIisseHandler.handleExternalPollMessage(ExternalPollMessageIisseHandler.java:67)
    at com.metaregistrarv2.schedule.PollTaskTimer.handleStoredMessages(PollTaskTimer.java:58)
    at com.metaregistrarv2.schedule.PollTaskTimer.execute(PollTaskTimer.java:38)
    at com.metaregistrarv2.schedule.EppPollerTimedTaskAdapter.execute(EppPollerTimedTaskAdapter.java:28)
    at com.metaregistrar.scheduling.TimedTask.run(TimedTask.java:22)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
    at com.metaregistrar.concurrent.PoolThread.run(PoolThread.java:26)
Caused by: javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25 (java.net.ConnectException: Connection refused))
    at javax.mail.Transport.send(Transport.java:163)
    at javax.mail.Transport.send(Transport.java:48)
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1391)
    ... 17 more
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25 (java.net.ConnectException: Connection refused)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:254)
    at javax.mail.Service.connect(Service.java:85)
    at javax.mail.Service.connect(Service.java:70)
    at javax.mail.Transport.send(Transport.java:94)
    ... 19 more
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
    ... 24 more
1

There are 1 answers

0
Nate Morrow On

I don't know if you got this figured out, but I was having the same problem. Here's what I found:

The email.send() calls Transport.send(), which was using the server's default settings for the outgoing socket binding for that SMTP port (I'm running JBoss EAP 6.2). It was configured to use localhost on port 25... so every time I tried to send mail, it was trying to send to localhost:25.

I changed settings for that socket binding and now everything works.