使用Office365发送javamail

Wil*_*ill 14 smtp jakarta-mail office365

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

Wil*_*ill 14

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

}
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,这只有在我将 `587` 更改为 `"587"` 后才有效。否则,JavaMail 会尝试通过端口 25 进行连接。 (2认同)

pou*_*sma 7

使用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
Run Code Online (Sandbox Code Playgroud)