Hi i am currently developing java application which will send mail to multiple recipients via Java Mail Api(1.6.2), i have configure the SMTP as per the Microsoft docs problem is the code is working with my personal hotmail email id but it fails for corporate office 365 account.
Error : javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful [PN1PR0101CA0066.INDPRD01.PROD.OUTLOOK.COM]
POP and IMAP are working(Receiving mails) and i can login with the password in Office 365 Web, i have tried changing password too.
Code :
User user = Credentials.ACC;
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.office365.com");//outlook.office365.com
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");//25
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.transport.protocol", "smtp");
//props.put("mail.smtp.ssl.enable", true);
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user.getUsername(), user.getPassword());
}
});
session.setDebug(true);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(user.getUsername());
msg.setRecipients(Message.RecipientType.TO,
"[email protected]");
msg.setSubject("Testing SMTP using [" + user.getUsername() + "]");
msg.setSentDate(new Date());
msg.setText("Hey, this is a test from [" + user.getUsername() + "], Sending via Java Mail API");
Transport.send(msg);
System.out.println("Sent Ok");
} catch (MessagingException e) {
System.out.println("Something went wrong");
e.printStackTrace();
}