使用Office365发送javamail

时间:2013-01-21 00:30:37

标签: smtp javamail office365

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

4 个答案:

答案 0 :(得分: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");          
      }       
  });

}

答案 1 :(得分:6)

使用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

答案 2 :(得分: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();
}

答案 3 :(得分: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");

更改主人,你会很好。