Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send javamail using Office365

I'm having trouble configuring the SMTP settings for sending mail using javax.mail (1.4.4) through Office365, so I thought I'd post the properties here for others.

like image 620
Will Avatar asked Jan 21 '13 00:01

Will


People also ask

Can we send email using Java?

Sending emails using Simple Java Mail is pretty straightforward. First, you need to create an email object using EmailBuilder . Then, you need to create a mailer object using MailerBuilder and pass the email object to the mailer object to send the email.


2 Answers

Use Office365 smtp details as below:

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

}
like image 107
Will Avatar answered Oct 13 '22 00:10

Will


And with spring-boot, you simply need to add this to your 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
like image 35
poussma Avatar answered Oct 13 '22 01:10

poussma