簡體   English   中英

為什么java郵件api會拋出異常?

[英]Why is java mail api throws exceptions?

任務:需要向用戶發送郵件。 庫: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
**

嘗試將端口更改為 465

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

異常:java.net.ConnectException

這意味着您的請求沒有在規定的時間內得到服務器的響應。 他們是這個例外的一些原因:

  • 太多請求使服務器過載

  • 由於網絡配置錯誤或線路過載導致請求丟包

  • 有時防火牆在服務器獲取之前消耗請求數據包

  • 還取決於線程連接池配置和連接池的當前狀態

  • 轉換期間丟失響應數據包

  • IP/域或端口不正確。 IP/域或端口(即服務)已關閉

  • IP/域的響應時間比您的默認超時要長。

  • 您有一個防火牆阻止您使用的任何端口上的請求或響應

  • 您有一個防火牆阻止對該特定主機的請求。

  • 你沒有連接到互聯網。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM