Send javamail using Office365
56,261
Solution 1
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"); } }); }
Solution 2
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
Solution 3
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();
}
Author by
Will
I know what dude I am. I'm the dude playin' the dude, disguised as another dude.
Updated on August 07, 2020Comments
-
Will over 3 years
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. -
Bilbo Baggins almost 10 yearsThe 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.. :) thanks
-
Glorfindel over 8 yearsFor me, this only worked after I changed
587
to"587"
. Otherwise, JavaMail tried to connect over port 25. -
JBert over 8 yearsIt would be better if the code in this answer were to use setProperty(String, String), this way you don't run into the problem @Glorfindel describes. Also, the Authenticator is not necessary if you use
transport.sendMessage(...)
on aTransport
instance for which you calledconnect(server, user, password)
first. -
JBert over 8 yearsYou might want to specify that this uses commons-email.
-
Emman Sun about 7 yearsI'm also facing thread stuck issue: DEBUG SMTP: trying to connect to host "smtp.office365.com", port 587, isSSL false
-
drvlas over 6 years@JBert : 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!
-
Dordoka Maisu over 4 years@Glorfindel Thank you so so much! This fixed my thread stuck issue. How the hell can this happen?