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程序导入certicate以生成jssecacerts文件,并将该文件添加到/jre/lib/security/path中。下面是我的工作代码
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");
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.starttls.enable","true");
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.host", "mail.xxxx.com");
props.put("mail.smtps.port", "587");
props.put("mail.transport.protocol.rfc822", "smtps");