Send email by java

1

I'm trying to send email via java but I'm 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 you give is in the mail.send () line:

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

I wanted to know why my error is in smtp 465 if I am putting 587.

    
asked by anonymous 03.08.2017 / 16:34

2 answers

0

You should 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);
    }
    
03.08.2017 / 17:12
0

I have an example that works perfectly, as follows:

   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: link

    
03.08.2017 / 18:33