Good morning. I'm having trouble sending an email using JavaMail.
Returns the following exception:
(javax.mail.SendFailedException) javax.mail.SendFailedException: Sending failed; nested exception is: class javax.mail.MessagingException: Exception reading response; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
I have already analyzed other questions, such as:
However, the error still persists.
Note: I've already enabled the option to allow less secure applications from my account in Gmail. In addition, using the Glassfish application manages to send the email. Apparently the problem is with Apache Tomcat.
Follow the code:
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
Session session1 = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("email", "senha");
}
});
/**
* Ativa Debug para sessão
*/
session1.setDebug(true);
try {
Message message = new MimeMessage(session1);
message.setFrom(new InternetAddress("enviaremail")); //Remetente
String destinatario = (String) session.getAttribute("email");
Address[] toUser = InternetAddress //Destinatário(s)
.parse(destinatario);
message.setRecipients(Message.RecipientType.TO, toUser);
message.setSubject("Novo E-mail!"); //Assunto
message.setText("Olá. Você recebeu um novo e-mail.");
/**
* Método para enviar a mensagem
* criada
*/
Transport.send(message);
System.out.println("Feito!!!");
} catch (MessagingException e) {
throw new RuntimeException(e);
}