how to send an email in java web with the javax.mail.jar library

0

I am using jsp, servlet and classes in java to send an email from a form, it works on the PC locally using payara 4.1 and netbeans from one gmail to another but when I upload the application to a java host do not send in the mail I have the application on the host

www.blancopropiedades.cl/pruebaJavaMail /

Servlet:

Email email = new Email();
boolean resultado = email.enviarCorreo(mensaje, asunto);

class in java:

public class Email {

public boolean enviarCorreo(String mensaje, String asunto){
    boolean enviado = false;
        try{
            String de = "[email protected]"; String clave = 
            "contraseña";
            String para="[email protected]";  
            String host = "smtp.gmail.com";

            Properties prop = System.getProperties();

            prop.put("mail.smtp.starttls.enable","true");
            prop.put("mail.smtp.host", host);
            prop.put("mail.smtp.user",de);
            prop.put("mail.smtp.password", clave);
            prop.put("mail.smtp.port",587);
            prop.put("mail.smtp.auth","true");

            Session sesion = Session.getDefaultInstance(prop,null);

            MimeMessage message = new MimeMessage(sesion);

            message.setFrom(new InternetAddress(de));

            message.setRecipient(Message.RecipientType.TO, new 
            InternetAddress(para));

            message.setSubject(asunto);
            message.setText(mensaje);

            Transport transport = sesion.getTransport("smtp");

            transport.connect(host,de,clave);

            transport.sendMessage(message, message.getAllRecipients());

            transport.close();

            enviado = true;

        }catch(Exception e){
            e.printStackTrace();
        }

    return enviado;
}
    
asked by jean carlos gonzalez valverde 07.01.2018 в 03:01
source

1 answer

1

You can use Spring Boot or download the library directly. This I have recently used in my web application and it works very well. What you have is that in the case of Gmail, allow client applications to send and receive mail, otherwise you will reject the remote connection.

dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

Then here is a code: Taken from baeldung

public JavaMailSender getJavaMailSender() {
JavaMailSender mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.gmail.com");
mailSender.setPort(587);

mailSender.setUsername("[email protected]");
mailSender.setPassword("password");

Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");

return mailSender;
}

Then send it:

public void sendSimpleMessage(
  String to, String subject, String text) {
    ...
    SimpleMailMessage message = new SimpleMailMessage(); 
    message.setTo(to); 
    message.setSubject(subject); 
    message.setText(text);
    emailSender.send(message);
    ...
}

more information you can check this link: Spring email. Configurations

    
answered by 07.01.2018 в 15:05