Error using JavaMail with Office365 account

1

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);
        }

    }

}
    
asked by anonymous 31.05.2015 / 17:41

1 answer

0

According to the message:

  

Could not connect to SMTP host: smtp.office365.com, port: 587; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

The problem is the use of SSL for traffic to a message on a channel that does not recognize it. By default Office 365 SMTP uses TLS explicitly, that is, the TLS (the mail.smtp.starttls.enable property) and you do not need SSL information.

So, some changes to make:

  • change from mail.smtp.starttls.enable to mail.smtp.starttls.enable
  • Do not enable SSL , remove all properties that contain mail.smtp.socketFactory , one of which is an SSL Socket Factory

At the end, the properties will look like this:

config.put("mail.smtp.auth", "true");
config.put("mail.smtp.starttls.enable", "true");
config.put("mail.smtp.host", "smtp.office365.com");
config.put("mail.smtp.port", 587);

From the rest, your code is OK. See a complete example on this gist .

PS: Office 365 Community is not working, there are some reference on the issue SSL / TLS . As soon as I get back I update this answer in more detail.

    
02.06.2015 / 19:00