Error sending email using Javamail

Asked

Viewed 405 times

2

Could someone help me fix this error? I can’t 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: Couldn’t connect to host, port: xxxxxxx.xxxxxx.org.br, 25; timeout -1; nested Exception is: 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] SMTP DEBUG: useEhlo true, useAuth false DEBUG SMTP: trying to connect to host "xxxxxxxx.xxxxxxxxx.org.br", port 25, isSSL false

2 answers

1

I think this is SMTP port problem, if I remember correctly operators blocked the door 25 in 2012 (correct me if I’m wrong).

Try the door 587

props.put("mail.smtp.socketFactory.port", "587");

0

The estate mail.smtp.ssl.trust can be used to explicitly say 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 of Javamail:

mail.smtp.ssl.trust - If set, and the socket Factory is not specified, enables the use of Mailsslsocketfactory. If set to "*", all hosts will be trusted. If set with a list of hosts separated by white space, that hosts will be trusted. Otherwise, the certificate is now trusted from what the server presents.

  • I changed the configuration and I left it this way... but it gives error, could you check why please? I will comment on the code again,

  • The same exception was made?

  • In the other answer you posted, the problem seems to be on your smtp server, check if it is UP.

  • What happens is that the other one had given several errors, so I switched the code, now it gives this error: 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.xxxxxxxxx.org.br", port 25, isSSL false , then he falls in the catch and introduces msg: com.sun.mail.util.Mailconnectexception: Couldn’t connect to host, port: xxxx.xxxxxx.org.br, 25; timeout -1; nested Exception is: java.net.Socketexception: Network is unreachable: connect

  • Check if your smtp host name is correct, if it is up. Try to ping.

  • I already pinged the SMTP I’m using, and it’s up, ta responding normally

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.