为什么Java Mail API会引发异常?

时间:2019-12-24 04:20:11

标签: java api email

任务:需要向用户发送邮件。 库:Java Mail 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
**

2 个答案:

答案 0 :(得分:1)

尝试将端口更改为465

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

答案 1 :(得分:0)

  

Exception:java.net.ConnectException

这意味着您的请求未在规定的时间内得到服务器的响应。它们是造成此异常的一些原因:

  • 太多请求使服务器超载

  • 由于错误的网络配置或线路而导致请求数据包丢失 超载

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

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

  • 过渡期间响应数据包丢失

  • IP /域或端口不正确。 IP /域或端口(即 服务)

  • IP /域花费的时间比默认超时时间长。

  • 您的防火墙阻止了 您正在使用的任何端口

  • 您的防火墙阻止了对该特定请求的请求 主机。

  • 您没有连接到互联网。