Properties props = new Properties();
props.put("mail.smtp.proxy.host","host");
props.put("mail.smtp.proxy.port","587");
props.put("mail.smtp.host", "smtp.office365.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.ssl.enable", "false");
// props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.fallback", "true");
Session session = Session.getInstance(props, new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username@domain.com", "password");
}
});
session.setDebug(true);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("This is the email subject");
message.setText("This is the email body");
Transport.send(message);
System.out.println("mailsent");
} catch (MessagingException mex) {
mex.printStackTrace();
}
I have written the above code for sending email using javamail API through SMTP but I ended up with the following exception. Please see my code below. I have used debugging mode and below the code you can find the exception as well.
Error Details
DEBUG SMTP: trying to connect to host "smtp.office365.com", port 587, isSSL false DEBUG SMTP: exception reading response: javax.net.ssl.SSLException: Unsupported or unrecognized SSL message javax.mail.MessagingException: Exception reading response;