Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaMail samples using Office365 SMTP
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendEmailOffice365 {
private static final Logger LOGGER = Logger.getAnonymousLogger();
private static final String SERVIDOR_SMTP = "smtp.office365.com";
private static final int PORTA_SERVIDOR_SMTP = 587;
private static final String CONTA_PADRAO = "mail@mail.com.br";
private static final String SENHA_CONTA_PADRAO = "password*";
private final String from = "mail@mail.com.br";
private final String to = "mail@mail.com";
private final String subject = "Teste";
private final String messageContent = "Teste de Mensagem";
public void sendEmail() {
final Session session = Session.getInstance(this.getEmailProperties(), new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(CONTA_PADRAO, SENHA_CONTA_PADRAO);
}
});
try {
final Message message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setFrom(new InternetAddress(from));
message.setSubject(subject);
message.setText(messageContent);
message.setSentDate(new Date());
Transport.send(message);
} catch (final MessagingException ex) {
LOGGER.log(Level.WARNING, "Erro ao enviar mensagem: " + ex.getMessage(), ex);
}
}
public Properties getEmailProperties() {
final Properties config = new Properties();
config.put("mail.smtp.auth", "true");
config.put("mail.smtp.starttls.enable", "true");
config.put("mail.smtp.host", SERVIDOR_SMTP);
config.put("mail.smtp.port", PORTA_SERVIDOR_SMTP);
return config;
}
public static void main(final String[] args) {
new SendEmailOffice365().sendEmail();
}
}
@simeonhearring

This comment has been minimized.

Copy link

@simeonhearring simeonhearring commented Nov 20, 2017

Worked like a charm!!

@brantansp

This comment has been minimized.

Copy link

@brantansp brantansp commented Mar 21, 2018

Hello,

How to resolve the below issue

WARNING: Error : Sending failed;
nested exception is:
class javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [AM5PR0202CA0009.eurprd02.prod.outlook.com]

javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [AM5PR0202CA0009.eurprd02.prod.outlook.com]

at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at l(SendEmailOffice365.java:50)
at (SendEmailOffice365.java:66)
@bhagyashreesharma

This comment has been minimized.

Copy link

@bhagyashreesharma bhagyashreesharma commented Jul 5, 2018

Hi,
I am having the same issue Client was not authenticated to send anonymous mail during MAIL FROM
Do you have any idea how to resolve ?

Thanks

@irudayambics

This comment has been minimized.

Copy link

@irudayambics irudayambics commented Aug 7, 2018

I am facing the same error. Do you have any solution? Please share it.

exception: javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful [SG2PR06CA0128.apcprd06.prod.outlook.com]**

@Waseem1983

This comment has been minimized.

Copy link

@Waseem1983 Waseem1983 commented Sep 12, 2018

It seems this app is working with any SMTP provider other than office365, it worked fine for me with Google and yahoo accounts but not office365!!
Sep 12, 2018 10:53:17 AM SendEmailOffice365 sendEmail
WARNING: Erro ao enviar mensagem: 535 5.7.3 Authentication unsuccessful [VI1P18901CA0001.EURP189.PROD.OUTLOOK.COM]

javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful [VI1P18901CA0001.EURP189.PROD.OUTLOOK.COM]

@SantoshBabar

This comment has been minimized.

Copy link

@SantoshBabar SantoshBabar commented Sep 21, 2018

I am too facing the same issue. Any solution over this?

@cromeroc-OC

This comment has been minimized.

Copy link

@cromeroc-OC cromeroc-OC commented Oct 8, 2018

good

@sujeetkalaskar

This comment has been minimized.

Copy link

@sujeetkalaskar sujeetkalaskar commented Feb 3, 2019

I am facing the below exception.
javax.mail.MessagingException: Could not connect to SMTP host: smtp.office365.com, port: 587;
nested exception is:
java.net.ConnectException: Connection timed out: connect

But using any online tool i am able to send the mail.
Please help to resolve this issue

@zubairmujeeb

This comment has been minimized.

Copy link

@zubairmujeeb zubairmujeeb commented Jun 9, 2021

hi any resolution?

@zubairmujeeb

This comment has been minimized.

Copy link

@zubairmujeeb zubairmujeeb commented Jun 9, 2021

I am facing the above same error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment