Skip to content

Instantly share code, notes, and snippets.

@brunocribeiro
Created June 2, 2015 00:05
Show Gist options
  • Save brunocribeiro/12a529f7f752f2853b9f to your computer and use it in GitHub Desktop.
Save brunocribeiro/12a529f7f752f2853b9f to your computer and use it in GitHub Desktop.
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 = "[email protected]";
private static final String SENHA_CONTA_PADRAO = "password*";
private final String from = "[email protected]";
private final String to = "[email protected]";
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();
}
}
@sujeetkalaskar
Copy link

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
Copy link

hi any resolution?

@zubairmujeeb
Copy link

I am facing the above same error.

@DaveB93
Copy link

DaveB93 commented Apr 7, 2022

To get this to work now, add
config.put("mail.smtp.ssl.protocols", "TLSv1.2");

@SalahEddineElBachiri
Copy link

@DaveB93 the same problem , i added the line you suggested

@Manikantan1991
Copy link

Still Same issue even after adding put("mail.smtp.ssl.protocols", "TLSv1.2");

Can some one Please help me.

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 535 5.7.139 Authentication unsuccessful, SmtpClientAuthentication is disabled for the Mailbox. Visit https://aka.ms/smtp_auth_disabled for more information. [MA0PR01CA0045.INDPRD01.PROD.OUTLOOK.COM 2023-05-23T14:34:28.089Z 08DB5B06995FDBBD]

at Outlook_Mail.sendMail.main(sendMail.java:166)

Caused by: javax.mail.AuthenticationFailedException: 535 5.7.139 Authentication unsuccessful, SmtpClientAuthentication is disabled for the Mailbox. Visit https://aka.ms/smtp_auth_disabled for more information. [MA0PR01CA0045.INDPRD01.PROD.OUTLOOK.COM 2023-05-23T14:34:28.089Z 08DB5B06995FDBBD]

@kgalarza95
Copy link

¿No hubo solución enotnces?

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