为什么当我setHostName(" mail.metaregistrar.com")并发送邮件以在localhost上使用SMTP时?
Java代码:
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();
异常堆栈跟踪:
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
答案 0 :(得分:0)
我不知道你是否弄明白,但我遇到了同样的问题。这是我发现的:
email.send()
调用Transport.send()
,它使用服务器对该SMTP端口的传出套接字绑定的默认设置(我正在运行JBoss EAP 6.2)。它被配置为在端口25上使用localhost ...所以每当我尝试发送邮件时,它都会尝试发送到localhost:25
。
我更改了套接字绑定的设置,现在一切正常。