我是代碼一個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程序上工作。
但它顯示此錯誤.... javax.mail.MessagingException:無法連接到SMTP主機:smtp.gmail.com,端口:465; 嵌套的異常是: \t javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路徑構建失敗: – 2015-01-31 19:13:43
您必須更改您的gmail帳戶中的安全參數(https://www.google.com/設置/安全),讓此應用程序訪問您的帳戶或使用Gmail API – esprittn 2015-01-31 19:36:07