16

我在配置javax.mail (1.4.4)通过 Office365 发送邮件的 SMTP 设置时遇到问题,所以我想我会在这里为其他人发布这些属性。

4

4 回答 4

21

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

}
于 2013-01-21T00:30:37.167 回答
10

使用 spring-boot,您只需将其添加到您的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
于 2016-09-29T14:04:17.677 回答
4

一个工作代码示例:

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();
}
于 2015-10-13T20:50:52.853 回答
0

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

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

换个主机就万事大吉了。

于 2015-09-23T21:14:01.143 回答