0

我试图连接到远程邮件服务器,但不知何故,公共电子邮件/JavaMail 总是尝试连接到本地主机。为什么???

    Email email = new SimpleEmail();
    email.setHostName("mailserver.com");
    email.setSentDate(new Date());
    email.setAuthentication("admin@mailserver.com", "password");
    email.setSubject("testing using simple mail");
    email.setMsg("test email");
    email.setFrom("*******@gmail.com");
    email.addTo("*****@gmail.com");
    email.setDebug(true);
    email.send();

我总是遇到以下异常:

    Exception in thread "main" org.apache.commons.mail.EmailException: Sending the email to the following server failed : ********:25
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)
    at org.apache.commons.mail.Email.send(Email.java:1267)
    at MyJasperService.main(MyJasperService.java:219)
Caused by: javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25 (java.net.ConnectException: Connection refused: connect))
    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:1232)
    ... 2 more
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25 (java.net.ConnectException: Connection refused: connect)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
    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)
    ... 4 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
    ... 9 more
4

1 回答 1

0

我似乎记得 Sun/Oracle 的其中一个 javamail 版本存在问题。

您使用的是哪个版本的 javamail(即 mail.jar)以及哪个版本的 commons-email?

如果您升级到两者的最新版本,也许它会起作用。

于 2011-12-22T16:39:10.977 回答