提问人:Raghed77 提问时间:9/30/2023 更新时间:9/30/2023 访问量:39
使用 Appache Commons 电子邮件库在 Java 中发送电子邮件时出现异常
exception while sending an email in java using Appache Commons email library
问:
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.office365.com:465
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1469)
at org.apache.commons.mail.Email.send(Email.java:1496)
at com.raghedabusaad.SimpleEmailExample.main(SimpleEmailExample.java:30)
Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.office365.com, 465; timeout 60000;
nested exception is:
java.net.ConnectException: Connection timed out: no further information
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1459)
... 2 more
Caused by: java.net.ConnectException: Connection timed out: no further information
at java.base/sun.nio.ch.Net.pollConnect(Native Method)
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:554)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:602)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
at java.base/java.net.Socket.connect(Socket.java:633)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:357)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:217)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175)
... 9 more
遇到此类异常的可能原因是什么?这是使用的代码:(地址和密码输入正确)。我还尝试关闭防火墙和防病毒软件,但仍然遇到相同的异常。(即使使用Gmail主机名)
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
public class SimpleEmailExample {
public static void main(String[] args) {
try {
Email email = new SimpleEmail();
email.setHostName("smtp.office365.com");
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("address", "password"));
email.setSSLOnConnect(true);
email.setFrom("address");
email.setSubject("appache commons trial");
email.setMsg("Appache commons trial successful");
email.addTo("address");
email.send();
System.out.println("Simple email sent successfully.");
} catch (EmailException e) {
e.printStackTrace();
---
}
}
}
答:
0赞
Noob
9/30/2023
#1
使用以下代码,我进行了 1 次修改。我替换了email.setSSLOnConnect(true);使用 email.setStartTLSEnabled(true);
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
public class EmailDemo {
public static void main(String[] args) {
try {
Email email = new SimpleEmail();
String address = "[email protected]";
email.setHostName("smtp.office365.com");
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator(address, "password"));
email.setStartTLSEnabled(true);
email.setFrom(address);
email.setSubject("appache commons trial");
email.setMsg("Appache commons trial successful");
email.addTo(address);
email.send();
System.out.println("Simple email sent successfully.");
} catch (EmailException e) {
e.printStackTrace();
}
}
}
我不得不将方法从 SSL 切换到 StartTLS,因为 587 端口适用于 StartTLS。此外,出于身份验证目的,请确保不要启用双因素身份验证;否则,您将遇到身份验证错误
端口 587 和 465 都经常用于 SMTPS 通信。端口 587 通常用于使用 STARTTLS 加密 SMTP 邮件,这允许电子邮件客户端通过请求邮件服务器通过 TLS 升级连接来建立安全连接。
评论