Send javamail using Office365
Asked Answered
A

5

18

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.

Ayrshire answered 21/1, 2013 at 0:30 Comment(0)
A
24

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

}
Ayrshire answered 21/1, 2013 at 0:30 Comment(7)
The problem I am facing is my thread got stuck before sending mail, I tried many solutions from internet non of them worked, I don't get any exception too. I am unable to send mail, it would be helpful if you share your knowledge.. :) thanksWingspan
For me, this only worked after I changed 587 to "587". Otherwise, JavaMail tried to connect over port 25.Nurse
It would be better if the code in this answer were to use setProperty(String, String), this way you don't run into the problem @Nurse describes. Also, the Authenticator is not necessary if you use transport.sendMessage(...) on a Transport instance for which you called connect(server, user, password) first.Lemuelah
I'm also facing thread stuck issue: DEBUG SMTP: trying to connect to host "smtp.office365.com", port 587, isSSL falseFranko
@Lemuelah : how can we create an instance transport of the abstract class Transport? Should we create a new child class?I have an issue with smtp.office365.com (Unknown server) and I'd like to separate connection and authentification from sending - as a debug. So your proposition to call connect() is interesting for me. Thanks!Overthrust
@Nurse Thank you so so much! This fixed my thread stuck issue. How the hell can this happen?Aqueous
After AUTH LOGIN error occour: 535 5.7.139 Authentication unsuccessful, the request did not meet the criteria to be authenticated successfully. Contact your administrator. 2023Iodometry
T
12

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
Tanya answered 29/9, 2016 at 14:4 Comment(0)
H
4

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();
}
Hideout answered 13/10, 2015 at 20:50 Comment(1)
You might want to specify that this uses commons-email.Lemuelah
C
0

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.

Commissary answered 23/9, 2015 at 21:14 Comment(0)
N
0

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);
        }
    }
}
Neoimpressionism answered 11/6, 2024 at 5:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.