Will
Will

Reputation: 6286

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.

Upvotes: 19

Views: 77200

Answers (5)

TanvirChowdhury
TanvirChowdhury

Reputation: 2445

javax.mail (1.4.4)(1.4.7) has some issues with oulook smtp .

the following code works for me using javax.mail(1.6.2)

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by tanvir on 6/11/24.
 */

public class OutlookMailSender {



    public static void main(String[] args) {

        String to = "[email protected]";
        String from = "your domain email address";
        final String username = "your domain email username"; // your Outlook email
        final String password = "your domain email passowrd"; // your Outlook email password

        // Assuming you are sending email through smtp-mail.outlook.com
        String host = "smtp-mail.outlook.com";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", "587");
        props.put("mail.debug", "true");
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.ssl.enable", "true");

        // Get the Session object.
        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

        try {

            Message message = new MimeMessage(session);


            message.setFrom(new InternetAddress(from));


            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));


            message.setSubject("Test Mail");


            message.setText("This is a test mail");


            Transport.send(message);

            System.out.println("Sent message successfully...");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

Upvotes: 0

Will
Will

Reputation: 6286

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

}

Upvotes: 24

poussma
poussma

Reputation: 7301

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

Upvotes: 12

A working code example:

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

Upvotes: 4

Abhishek Shah
Abhishek Shah

Reputation: 77

The only error that I am noticing in your code is the incorrect Host

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

Change the host you will be all good.

Upvotes: 0

Related Questions