【发布时间】:2015-02-16 22:41:31
【问题描述】:
我正在尝试获取 this example 以使 Apache Commons 电子邮件库正常工作。这是我的代码:
SimpleEmail email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username@gmail.com", "password"));
email.setTLS(true);
try {
email.setFrom("username@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("username@gmail.com");
System.out.println("Sending...");
email.send();
System.out.println("Email sent!");
} catch (Exception e) {
System.out.println("Email not sent!");
e.printStackTrace();
}
如您所见,它与示例基本没有变化,除了我必须使用端口 465 而不是 587,因为 587 会导致 Connection refused 异常(基于 this question)。现在这段代码挂在email.send() 行上。我得到的唯一输出是:
Sending...
但不会抛出异常。我需要在防火墙中打开一个端口吗? (我可能无法做到这一点,因为我正试图从工作中做到这一点)。谢谢!
编辑
很长一段时间后我得到了这个异常:
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465
...
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
【问题讨论】:
-
如果您尝试从命令行远程登录,可以吗? $ telnet smtp.gmail.com 465 // 如果你运行的是linux,没问题,windows,可能需要添加telnet程序,到控制面板添加到windows组件中即可。
-
我不能。 telnet 挂起一段时间,然后静默失败。但是,请参阅我上面的编辑。
标签: java email apache-commons-email