Error when sending e-mail via production

Asked

Viewed 94 times

1

I’m using javaxmail to send the e-mail via system, but locally I can send the email without problems, but when I put it on the server it returns me 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 user and password are correct. I will 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 "";

    }

}

1 answer

0

Browser other questions tagged

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