2015-01-31 160 views
0

我是代碼一個Java電子郵件發送程序。但是當我點擊發送按鈕時,按鈕出現懸掛模式&程序仍在運行,但郵件沒有發送。 我無法檢測到問題。任何人都可以幫助我... 代碼如下。Java電子郵件發送API電子郵件沒有發送..掛斷程序

Properties props = new Properties(); 
    props.put("mail.transport.protocol", "smtp"); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.socketFactory.port", "465"); 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.starttls.enable", "false"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.port", "465"); 

    Session session = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication("[email protected]", "password"); 
       } 
      } 
    ); 

    try { 
     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]")); 
     message.setSubject("Demo mail"); 
     message.setText("Hello, world!"); 
     Transport.send(message); 

     JOptionPane.showMessageDialog(this, "Message sent!"); 
    } catch (Exception e) { 
     JOptionPane.showMessageDialog(this, e); 
    } 

我的電子郵件帳戶尚未激活兩步驗證服務。 它也在Outlook電子郵件發送軟件中工作..我測試過。

但不能在我的java程序上工作。

回答

0

我相信Authenticator類應該擴展。這裏是一個適用於我的例子:

public class SendEmail { 

    public SendEmail() {} 

    public void send (String text){ 
     String host = "smtp.gmail.com"; 
     String username = "[email protected]"; 
     String password = "password"; 
     Properties props = new Properties(); 
     // set any needed mail.smtps.* properties here 
     Session session = Session.getInstance(props, new GMailAuthenticator("user", "password")); 
     Message msg = new MimeMessage(session); 
     Transport t; 
     try { 
      msg.setText(text); 
      msg.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]", "Stack King")); 
      t = session.getTransport("smtps"); 
      t.connect(host, username, password); 
      t.sendMessage(msg, msg.getAllRecipients()); 
      t.close(); 
      Gdx.app.log("Email", "Message sent successfully."); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    class GMailAuthenticator extends Authenticator { 
     String user; 
     String pw; 
     public GMailAuthenticator (String username, String password) 
     { 
      super(); 
      this.user = username; 
      this.pw = password; 
     } 
     public PasswordAuthentication getPasswordAuthentication() 
     { 
      return new PasswordAuthentication(user, pw); 
     } 
    } 
} 
+0

但它顯示此錯誤.... javax.mail.MessagingException:無法連接到SMTP主機:smtp.gmail.com,端口:465; 嵌套的異常是: \t javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路徑構建失敗: – 2015-01-31 19:13:43

+0

您必須更改您的gmail帳戶中的安全參數(https://www.google.com/設置/安全),讓此應用程序訪問您的帳戶或使用Gmail API – esprittn 2015-01-31 19:36:07