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