Send email by java

Asked

Viewed 270 times

1

I am trying to send mail by java but am having an error with smtp. My code:

try {
        SimpleEmail mail = new SimpleEmail();

        mail.setFrom("[email protected]", "Teste");
        mail.setSubject("E-mail exemplo");
        mail.setMsg("E-mail de exemplo");
        email.setStartTLSEnabled(true);
        mail.setAuthentication("[email protected]", "********");
        mail.setHostName("smtp.gmail.com");

        mail.addTo("[email protected]", "Lucas Souza");
        mail.setSmtpPort(587);
        mail.send();

    } catch (EmailException ex) {
        Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, ex);
    }

The error is in the mail line.send():

org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:467

I wonder why my mistake is in smtp 465 if I’m putting 587.

2 answers

0

You must set TLS instead of SSL:

try {
        SimpleEmail mail = new SimpleEmail();

        mail.setFrom("[email protected]", "Teste");
        mail.setSubject("E-mail exemplo");
        mail.setMsg("E-mail de exemplo");
        mail.setStartTLSEnabled(true);
        mail.setAuthentication("[email protected]", "********");
        mail.setHostName("smtp.gmail.com");

        mail.addTo("[email protected]", "Lucas Souza");
        mail.setSmtpPort(587);
        mail.send();

    } catch (EmailException ex) {
        Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, ex);
    }
  • Improved, now it at least gives the error in port 587 aheuhaueh

0

I have an example that works perfectly, below:

   public static void sendEmail() throws EmailException {

        SimpleEmail email = new SimpleEmail();
        // Utilize o hostname do seu provedor de email
        System.out.println("alterando hostname...");
        email.setHostName("smtp.gmail.com");

        // Quando a porta utilizada não é a padrão (gmail = 465)
        email.setSmtpPort(465);

        // Adicione os destinatários
        email.addTo("[email protected]");

        // Configure o seu email do qual enviará
        email.setFrom("[email protected]", "Karan User");

        // Adicione um assunto
        email.setSubject("Lembrete de senha");

        // Adicione a mensagem do email
        email.setMsg("Lembrete de senha karanalpe ");

        // Para autenticar no servidor é necessário chamar os dois métodos abaixo
        System.out.println("autenticando...");
        email.setSSL(true);
        email.setAuthentication("[email protected]", "suaSenha");
        System.out.println("enviando...");
        email.send();
        System.out.println("Email enviado!");
    }

No pom.xml:

   <dependencies>      
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.2</version>
        </dependency>   

        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>     

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.6</version>
        </dependency> 
    </dependencies> 

See post: http://karanalpe.com.br/tecnologia/back-end/enviando-email-com-o-java/

  • You keep making the same mistake here =(

  • Lucas... I just tested it on my machine and it worked. Did you open the post I made available? Download this project and test: https://github.com/karanalpe/envio-email

  • Don’t forget to change this data: "email.setAuthentication("[email protected]", "sweatNew");"

Browser other questions tagged

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