web-dev-qa-db-fra.com

Envoyer javamail en utilisant Office365

Je ne parviens pas à configurer les paramètres SMTP pour l'envoi de courrier à l'aide de javax.mail (1.4.4) via Office365;.

14
Will

Utilisez les détails smtp Office365 comme ci-dessous:

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

}
14
Will

Et avec spring-boot, il vous suffit d'ajouter ceci à votre application.properties:

spring.mail.Host = smtp.office365.com
spring.mail.username = [email protected]
spring.mail.password = s3cr3t
spring.mail.port = 587
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.starttls.enable = true
6
poussma

Un exemple de code de travail: 

Email email = new SimpleEmail();

email.setHostName("smtp.office365.com");
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("[email protected]", "****"));
email.setStartTLSEnabled(true);
try {
    email.setFrom("[email protected]");
    email.setSubject("Job Failure");
    email.setDebug(true);
    email.setMsg("This is a test mail ... :-)" );
    email.addTo("[email protected]");
    email.send();
} catch (EmailException e) {
    e.printStackTrace();
}

La seule erreur que je remarque dans votre code est un hôte incorrect

javaMailProperties.setProperty("mail.smtp.from", "[email protected]");
    javaMailProperties.setProperty("mail.smtp.user",  "[email protected]");
    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");

Changer l'hôte, vous serez tous bons.

0
Abhishek Shah