我正在尝试通过 TLS 连接从我的程序发送电子邮件。这是我的代码
final String username = "XXXXXX";
final String password = "XXXXX";
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "mail.xxxx.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@xxxx.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to_address));
message.setSubject("Test Mail");
message.setText("TestMail ");
Transport.send(message)
我的电子邮件网关在端口 587 上启用了启用 SSL 的传入邮件设置和启用了 TLS 的传出邮件设置。我可以在 Outlook 中配置此设置并且工作正常。但是通过我的java程序它说“连接被拒绝”。帮助赞赏!
最后工作:
我使用InstallCert程序导入证书以生成 jssecacerts 文件,并将该文件添加到我的 /jre/lib/security/ 路径中。这是我的工作代码
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", "XXXXXX");
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.ssl.enable", true);
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");
properties.put("mail.smtp.quitwait", "false");
properties.put("mail.smtp.auth", "true");