为什么 Java 邮件 API 会抛出异常?



任务:需要向用户发送邮件。 库:java 邮件 api 1.6.2

程序代码:

final String username = "ns**t@gmail.com";
final String password = "x9G";
final String to = "to@hhh.bla";
final String host = "smtp.gmail.com";
final String from ="ns@gmail.com";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);

// Set From: header field of the header.
message.setFrom(new InternetAddress(from));

// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));

// Set Subject: header field
message.setSubject("Testing Subject");

// Now set the actual message
message.setText("Hello, this is sample for to check send "
+ "email using JavaMailAPI ");

// Send message
Transport.send(message);

System.out.println("Sent message successfully....");

} catch (MessagingException e) {
throw new RuntimeException(e);
}

但是,这会引发异常:

> **Exception in thread "main" java.lang.RuntimeException: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at ExcelID.ExcelArtifact.App.main(App.java:174)
Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect
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 ExcelID.ExcelArtifact.App.main(App.java:169)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:359)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175)
... 7 more
**

尝试将端口更改为 465

props.put("mail.smtp.port", "465");
Exception

: java.net.ConnectException

这意味着您的请求没有在规定的时间内从服务器获得响应。此异常有一些原因:

  • 请求过多,使服务器过载

  • 由于网络配置或线路错误而请求丢包 超载

  • 有时防火墙在服务器获取之前会消耗请求数据包

  • 还取决于线程连接池配置和当前 连接池的状态

  • 转换期间丢失的响应数据包

  • IP/域或端口不正确。 IP/域或端口(即 服务(已关闭

  • IP/域的响应时间比默认超时长。

  • 您的防火墙阻止了 无论您使用什么端口

  • 您有一个防火墙阻止对该特定请求 主机。

  • 您未连接到互联网。

最新更新