使用Office365发送的JavaMail(Send javamail using Office3

2019-07-18 11:33发布

我无法配置使用发送邮件的SMTP设置javax.mail (1.4.4)通过Office365,所以我想我会在这里发表的属性等。

Answer 1:

使用Office365 SMTP详细信息如下:

 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"); } }); } 


Answer 2:

而随着春天开机,你只需要添加到您的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


Answer 3:

一个工作代码示例:

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();
}


Answer 4:

我在你的代码我注意到唯一的错误是不正确的主机

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");

更改主机,你会被大家看好。



文章来源: Send javamail using Office365