I have a Java class that works perfectly for sending emails through a Gmail account. However, even using the recommended settings to send an SMTP email with Office365, an error is returned.
The error is as follows:
exception javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.office365.com, port: 587; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
Here is the code for my class:
public class GmailBean {
public static final String SERVIDOR_SMTP = "smtp.office365.com";
public static final int PORTA_SERVIDOR_SMTP = 587;
private static final String CONTA_PADRAO = "[email protected]";
private static final String SENHA_CONTA_PADRAO = "xxx";
private String de;
private String para;
private String assunto;
private String mensagem;
public void enviarEmail() throws MessagingException {
FacesContext context = FacesContext.getCurrentInstance();
AutenticaUsuario autenticaUsuario = new AutenticaUsuario(GmailBean.CONTA_PADRAO, GmailBean.SENHA_CONTA_PADRAO);
Session session = Session.getInstance(this.configuracaoEmail(), autenticaUsuario);
// try{
Transport envio = null;
MimeMessage email = new MimeMessage(session);
email.setRecipient(Message.RecipientType.TO, new InternetAddress(this.para));
email.setFrom(new InternetAddress(this.de));
email.setSubject(this.assunto);
email.setContent(this.mensagem, "text/plain");
email.setSentDate(new Date());
envio = session.getTransport("smtp");
envio.connect(GmailBean.SERVIDOR_SMTP, GmailBean.CONTA_PADRAO, GmailBean.SENHA_CONTA_PADRAO);
email.saveChanges();
envio.sendMessage(email, email.getAllRecipients());
envio.close();
context.addMessage(null, new FacesMessage("Mensagem enviada com sucesso!"));
/* }
catch(AddressException ex)
{ Logger logger = Logger.getAnonymousLogger();
FacesMessage msg = new FacesMessage("Erro ao enviar mensagem "+ ex.getMessage());
logger.info("Erro ao enviar mensagem _____________"+ ex.getMessage());
}
catch(MessagingException ex)
{
Logger logger = Logger.getAnonymousLogger();
FacesMessage msg = new FacesMessage("Erro ao enviar mensagem "+ ex.getMessage());
logger.info("Erro ao enviar mensagem _____________"+ ex.getMessage());
}*/
}
public Properties configuracaoEmail() {
Properties config = new Properties();
config.put("mail.smtp.auth", "true");
config.put("mail.transport.protocol", "smtp");
config.put("mail.smtp.starttls.enabled", "true");
config.put("mail.smtp.host", SERVIDOR_SMTP);
config.put("mail.user", GmailBean.CONTA_PADRAO);
config.put("mail.smtp.port", PORTA_SERVIDOR_SMTP);
config.put("mail.smtp.socketFactory.port", PORTA_SERVIDOR_SMTP);
config.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
config.put("mail.smtp.socketFactory.fallback", "false");
return config;
}
// getters and setters
class AutenticaUsuario extends Authenticator {
private String usuario;
private String senha;
public AutenticaUsuario(String usuario, String senha) {
this.usuario = usuario;
this.senha = senha;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.usuario, this.senha);
}
}
}