I'm having trouble configuring the SMTP settings for sending mail using javax.mail (1.4.4)
through Office365, so I thought I'd post the properties here for others.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Use Office365 smtp details as below:
private static Properties props; private static Session session; static { props = new Properties(); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.host", "m.outlook.com"); props.put("mail.smtp.auth", "true"); session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("office365 email address", "office365 password"); } }); }
回答2:
And with spring-boot, you simply need to add this to your application.properties
:
spring.mail.host = smtp.office365.com
spring.mail.username = mathieu.pousse@headquarter.com
spring.mail.password = s3cr3t
spring.mail.port = 587
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.starttls.enable = true
回答3:
A working code example:
Email email = new SimpleEmail();
email.setHostName("smtp.office365.com");
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("a@b.com", "****"));
email.setStartTLSEnabled(true);
try {
email.setFrom("a@b.com");
email.setSubject("Job Failure");
email.setDebug(true);
email.setMsg("This is a test mail ... :-)" );
email.addTo("a@y.com");
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
回答4:
The only error that I am noticing in your code is the incorrect Host
javaMailProperties.setProperty("mail.smtp.from", "abc@c.com");
javaMailProperties.setProperty("mail.smtp.user", "abc@c.com");
javaMailProperties.setProperty("mail.smtp.password","Password");
javaMailProperties.setProperty("mail.smtp.host", "smtp.office365.com");
javaMailProperties.setProperty("mail.smtp.port", "587");
javaMailProperties.setProperty("mail.smtp.auth", "true");
javaMailProperties.setProperty("mail.smtp.starttls.enable", "true");
Change the host you will be all good.