无法从服务器通过 SMTP 发送邮件,但我可以从本地主机发送邮件
Cannot send mail over SMTP from server but I can from localhost
当我尝试在我的 Java 应用程序中通过 SMTP 发送电子邮件时,我收到以下信息。我是 运行 我的 Windows Server 2012 R2 上的胖 JAR 中的 Springboot 应用程序。
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Apr 09 22:16:57 CEST 2017
There was an unexpected error (type=Internal Server Error, status=500).
Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: internetsmtp.test.com, 25; timeout -1; nested exception is: java.net.UnknownHostException: internetsmtp.test.com. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: internetsmtp.test.com, 25; timeout -1; nested exception is: java.net.UnknownHostException: internetsmtp.test.com
代码:
public void sendEmail(String from, String replyTo, String to, String subject, String body) {
final Email email;
try {
email = DefaultEmail.builder()
.from(new InternetAddress(from))
.replyTo(new InternetAddress(replyTo))
.to(Lists.newArrayList(new InternetAddress(to)))
.subject(subject)
.body(body)
.encoding("UTF-8").build();
emailService.send(email);
} catch (AddressException e) {
log.error("MailUtil got exception while sending mail = " + e.getMessage());
}
}
application.properties:
# Server
server.port=8080
# Mail Config
spring.mail.host: internetsmtp.test.com
spring.mail.port: 25
spring.mail.username: user123
spring.mail.password: test123
spring.mail.properties.mail.smtp.auth: true
spring.mail.properties.mail.smtp.starttls.enable: false
spring.mail.properties.mail.smtp.starttls.required: false
spring.mail.persistence.enabled: false
spring.mail.persistence.redis.embedded: false
spring.mail.persistence.redis.enabled: false
spring.mail.scheduler.priorityLevels=10
我什至试过信任证书:
keytool -import -file "C:\mail.cer" -keystore "C:\Program Files\Java\jre.1.8.0_121\lib\security\cacerts" -alias mail
我可以从 localhost 毫无问题地完成所有这些工作。是否可能是我的服务器阻止了与 SMTP 服务器的连接?
您的例外清楚地说明 - java.net.UnknownHostException: internetsmtp.test.com
。这意味着(在这种情况下)您的应用程序无法从服务器解析 DNS 名称。确保您能够从服务器本身 ping 该域,并且该服务器的 DNS 访问(端口 53)未被阻止
很可能是防火墙阻止了直接连接。 JavaMail FAQ 有更多 tips for debugging connection problems.
当我尝试在我的 Java 应用程序中通过 SMTP 发送电子邮件时,我收到以下信息。我是 运行 我的 Windows Server 2012 R2 上的胖 JAR 中的 Springboot 应用程序。
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Apr 09 22:16:57 CEST 2017
There was an unexpected error (type=Internal Server Error, status=500).
Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: internetsmtp.test.com, 25; timeout -1; nested exception is: java.net.UnknownHostException: internetsmtp.test.com. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: internetsmtp.test.com, 25; timeout -1; nested exception is: java.net.UnknownHostException: internetsmtp.test.com
代码:
public void sendEmail(String from, String replyTo, String to, String subject, String body) {
final Email email;
try {
email = DefaultEmail.builder()
.from(new InternetAddress(from))
.replyTo(new InternetAddress(replyTo))
.to(Lists.newArrayList(new InternetAddress(to)))
.subject(subject)
.body(body)
.encoding("UTF-8").build();
emailService.send(email);
} catch (AddressException e) {
log.error("MailUtil got exception while sending mail = " + e.getMessage());
}
}
application.properties:
# Server
server.port=8080
# Mail Config
spring.mail.host: internetsmtp.test.com
spring.mail.port: 25
spring.mail.username: user123
spring.mail.password: test123
spring.mail.properties.mail.smtp.auth: true
spring.mail.properties.mail.smtp.starttls.enable: false
spring.mail.properties.mail.smtp.starttls.required: false
spring.mail.persistence.enabled: false
spring.mail.persistence.redis.embedded: false
spring.mail.persistence.redis.enabled: false
spring.mail.scheduler.priorityLevels=10
我什至试过信任证书:
keytool -import -file "C:\mail.cer" -keystore "C:\Program Files\Java\jre.1.8.0_121\lib\security\cacerts" -alias mail
我可以从 localhost 毫无问题地完成所有这些工作。是否可能是我的服务器阻止了与 SMTP 服务器的连接?
您的例外清楚地说明 - java.net.UnknownHostException: internetsmtp.test.com
。这意味着(在这种情况下)您的应用程序无法从服务器解析 DNS 名称。确保您能够从服务器本身 ping 该域,并且该服务器的 DNS 访问(端口 53)未被阻止
很可能是防火墙阻止了直接连接。 JavaMail FAQ 有更多 tips for debugging connection problems.