【发布时间】:2018-10-22 09:10:26
【问题描述】:
我正在尝试从在 Apache 上运行 Tomcat 的开发计算机发送电子邮件。我正在 Windows 上开发,但其他人正在 Macintosh 或 Linux 上开发。我无法确定要使用哪个主机和端口。我不知道密码是从哪里来的。
public void sendPackage(String toEmailAddress, String subject, String content)
{
Url url = ((WebRequest)RequestCycle.get().getRequest()).getUrl();
String fullUrl = RequestCycle.get().getUrlRenderer().renderFullUrl(url);
if (fullUrl.toLowerCase().contains("localhost"))
{
sHost = "localhost"; //"smtpout.secureserver.net";
iPort = 465; //25; //9090;
sFrom = "info@mywebsite.com";
}
else
{
sHost = "mail.mywebsite.com";
iPort = 587;
sFrom = "mail@mywebsite.com";
}
try
{
Email email = new SimpleEmail();
email.setDebug(true);
email.setHostName(sHost);
email.setSmtpPort(iPort);
email.setAuthenticator(new DefaultAuthenticator(sFrom, "tuscul312"));
email.setSSLOnConnect(true);
email.setFrom(sFrom);
email.setSubject(subject);
email.setMsg(content);
email.addTo(toEmailAddress);
email.send();
}
catch (EmailException eex)
{
logger.error("sendPackage failed: " + eex);
eex.printStackTrace();
}
}
堆栈跟踪显示
[ERROR] 2018-05-11 17:50:37.096 [http-nio-8080-exec-1] SendEmail - sendPackage failed: org.apache.commons.mail.EmailException: Sending the email to the following server failed : localhost:465
org.apache.commons.mail.EmailException: Sending the email to the following server failed : localhost:465
和
Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 465; timeout 60000;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2118)
这种硬编码配置应该有效吗? 其他地方有服务器设置吗?
【问题讨论】:
-
虽然 Windows 可能没有本地 SMTP 服务器,但基于 Unix 的操作系统仍然需要 SMTP 中继。您不能再从任何地方发送到任何地方 - 开放 SMTP 中继的日子已经一去不复返了。但是,您可以使用 Gmail - 有关详细信息,请参阅 the settings page。