java JavaMail API 的 Outlook 电子邮件设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38146683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Outlook Email Setup for JavaMail API
提问by Lance Zhao Zai
I having headache in setting up the outlook mail to JavaMail, I had followed the instructions from hereand here
我在设置 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("[email protected]"));
Transport.send(msg);
}catch (MessagingException mex) {
mex.printStackTrace();
}
And I keep getting the errors:
我不断收到错误:
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)
I had searching around google and stackoverflow, I guess it is a MUST to use SSL instead of TLS because outlook doesn't accept TLS or else the connection will be close by remote host, but the solutions I found from Internet didn't work out. What am I missed in order to send my testing mail successfully?
我在 google 和 stackoverflow 上搜索过,我想必须使用 SSL 而不是 TLS,因为 Outlook 不接受 TLS 否则连接将被远程主机关闭,但是我从 Internet 找到的解决方案没有奏效. 为了成功发送我的测试邮件,我错过了什么?
I am total new to mail API, any advises or helps will be much appreciated. Thanks.
我是邮件 API 的新手,任何建议或帮助将不胜感激。谢谢。
EDIT:I have a email account from microsoft I registered similar to '[email protected]'
编辑:我有一个来自微软的电子邮件帐户,我注册的类似于“[email protected]”
采纳答案by Angel Cuenca
Delete the SocketFactory parameters like this, and enjoy sending emails.
像这样删除 SocketFactory 参数,享受发送电子邮件的乐趣。
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");
I didn't find this parameters necessary on the documentation.
我在文档中没有发现这个参数是必需的。
https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html
https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html
回答by Christoph-Tobias Schenke
Please retry without these props:
请在没有这些道具的情况下重试:
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "true");
I do not have to set them for office365. outlook.com should be similiar
我不必为 office365 设置它们。Outlook.com 应该是类似的
回答by Menai Ala Eddine
Try this code it is worked for me
试试这个代码它对我有用
Properties props = null;
if (props == null) {
props = new Properties();
props.put("mail.smtp.auth", true);
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.host", "smtp.live.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.user", User);
props.put("mail.smtp.pwd", Pwd);
}
Session session = Session.getInstance(props, null);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(User));
if (Objet != null) {
msg.setSubject(Objet);
}
msg.setText(Content);
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(emailId));
Transport transport = session.getTransport("smtp");
transport.connect("smtp.live.com", 587, User, Pwd);
transport.sendMessage(msg, msg.getAllRecipients());
System.out.println("Mail sent successfully at " + emailId);
transport.close();
}