我在将 outlook 邮件设置为 JavaMail 时感到头疼,我已经按照 此处 和 此处 的说明进行操作
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", "smtp-mail.outlook.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "true");
try
{
Authenticator auth = new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(d_email, d_password);
}
};
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setText("Hey, this is the testing email.");
msg.setSubject("Testing");
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("example@gmail.com"));
Transport.send(msg);
}catch (MessagingException mex) {
mex.printStackTrace();
}
而且我不断收到错误:
javax.mail.MessagingException: Could not connect to SMTP host: smtp-mail.outlook.com, port: 587;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
我在 google 和 stackoverflow 上搜索过,我想必须使用 SSL 而不是 TLS,因为 outlook 不接受 TLS 否则连接将被远程主机关闭,但我从互联网上找到的解决方案没有成功.为了成功发送我的测试邮件,我错过了什么?
我对邮件 API 是全新的,任何建议或帮助将不胜感激。谢谢。
编辑: 我有一个来自微软的电子邮件帐户,我注册的类似于“xxxx@outlook.com”
原文由 Lance Leroy 发布,翻译遵循 CC BY-SA 4.0 许可协议
像这样删除 SocketFactory 参数,并享受发送电子邮件的乐趣。
我没有在文档中找到必要的参数。
https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html