Error sending email using JavaMail

2

Could someone help me fix this error? I can not send the email to the inbox.

Follow the code below:

@Service
public class EnvioEmailServicoImpl implements EnvioEmailServico {

    @Override
    public void enviarEmail(String id){

        String destinatarioEmail = null;    

        Properties props = new Properties();
        props.put("mail.smtp.host", "xxxxxx.xxxxxxxx.org.br");//
        props.put("mail.smtp.socketFactory.port", "25");//465
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "false");
        props.put("mail.smtp.port", "25");      

        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                //return new PasswordAuthentication("", "");
                return null;
            }
        });
        session.setDebug(true); 

        FabricaConexao fabricaConexao = new FabricaConexao();

        Connection connection = fabricaConexao.getConexao();

        String sql = "select fun_email from tbl_funcionario where fun_codigo = ?";

        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, id);
            ResultSet resultSet = statement.executeQuery();

            while (resultSet.next()) {
                Funcionario fun = new Funcionario();
                fun.setEmail(resultSet.getString("fun_email"));
                destinatarioEmail = fun.getEmail();
            }
        } catch (SQLException sE) {
            FacesUtil.adicionaMensagemErro("Erro no SQL: " + sE);
        } catch (Exception ex) {
            FacesUtil.adicionaMensagemErro("Erro :" + ex);
        }       

        String remetente = "[email protected]";//email do administrador
        String destinatario =  destinatarioEmail;//email do funcionário que solicitou nova senha    

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(remetente));// Remetente
            Address[] toUser = InternetAddress.parse(destinatario);// Destinatarios
            message.setRecipients(Message.RecipientType.TO, toUser);
            message.setSubject("Gerar senha");// assunto
            message.setText("Segue abaixo link para realizar a troca de sua senha!!");
            message.setContent("Você solicitou alterar sua Senha de acesso,</br> Para criar nova senha, clique no link abaixo: <br/>"+"<html><a href=\"localhost:8080/sgc/pages/gerarSenha.xhtml?id="+id+"\">"
                    + "http://localhost:8080/sgc/pages/gerarSenha.xhtml </a></html>", "text/html");
            Transport.send(message);    

        } catch (Exception e) {
            FacesUtil.adicionaMensagemErro("Erro ao tentar enviar email: "+ e);
        }
    }
    }

Catch error:

  

Error trying to send email: com.sun.mail.util.MailConnectException:   Could not connect to host, port: xxxxxxx.xxxxxxxxx.org.br, 25; timeout   -1; nested exception: java.net.SocketException: Network is unreachable: connect

JavaMail debug error:

  

DEBUG: setDebug: JavaMail version 1.5.5 DEBUG: getProvider () returning   javax.mail.Provider [TRANSPORT, smtp, com.sun.mail.smtp.SMTPTransport, Oracle]   DEBUG SMTP: useEhlo true, useAuth false DEBUG SMTP: trying to connect   to host "xxxxxxxx.xxxxxxxxxxxx.org.br", port 25, isSSL false

    
asked by anonymous 08.03.2016 / 16:05

2 answers

1

I think this is an SMTP port issue, if I remember correctly the carriers blocked port 25 in 2012 (correct me if I'm wrong).

Try port 587

props.put("mail.smtp.socketFactory.port", "587");
    
02.08.2016 / 16:51
0

The mail.smtp.ssl.trust quer property can be used to explicitly state that you must trust the smtp host certificate.

In your case:

props.put("mail.smtp.ssl.trust", "xxxxxxx.xxxxxxx.org.br");

Translated from the documentation for JavaMail : / p>

  

mail.smtp.ssl.trust - If set, and socket factory is not specified, enable MailSSLSocketFactory . If set to "*", all hosts will be trusted. If set to a hosts list separated by white space, this hosts will be trusted. Otherwise, the certificate becomes trusted from the server.

    
09.03.2016 / 07:42