我是代码一个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