Error sending production email

1

I'm using javaxmail to send the email via the system, but locally I can send the email without problems, but when I put it on the server it returns the error:

  

java.lang.RuntimeException: javax.mail.AuthenticationFailedException

Speaking that failed to authenticate, but the same code that is running locally is running on the server, and the username and password are correct. I'll leave below the code for sending email:

package Uteis;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EnviarEmail {

    private Session session;

    public EnviarEmail() {

        ResourceBundle resourceBundle;
        resourceBundle = ResourceBundle.getBundle("dadosEmail", Locale.getDefault());

        final String username = resourceBundle.getString("email");

        final String password = resourceBundle.getString("senha");
        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });

    }

    public String enviarEmail(DadosUsuarioEmail dados) {

        try {

            Message message = new MimeMessage(session);
            // message.setFrom(new InternetAddress(dados.getEmail()));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(dados.getEmail()));
            message.setSubject(dados.getSubject());
            message.setText(dados.getMessage());

            Transport.send(message);

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

        return "";

    }

}
    
asked by anonymous 30.08.2017 / 19:53

1 answer

0

Maybe your gmail account is not allowing external applications.

Try the following:

1. Fazer o login no gmail
2. Acessar a url https://accounts.google.com/DisplayUnlockCaptcha
3. Pressionar o botão "Continuar" para desbloquear o captcha

From now on your account is being sent sending emails in other applications.

Ref: link

    
30.08.2017 / 20:42