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
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,
– Marquin Ferreira
The same exception was made?
– caiomaia
In the other answer you posted, the problem seems to be on your smtp server, check if it is UP.
– caiomaia
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
– Marquin Ferreira
Check if your smtp host name is correct, if it is up. Try to ping.
– caiomaia
I already pinged the SMTP I’m using, and it’s up, ta responding normally
– Marquin Ferreira