Отправка писем с помощью Mailtrap при сбое автоматического скрипта (Selenium - Java) - PullRequest
1 голос
/ 29 мая 2020

Привет, сообщество: я пытаюсь автоматизировать рассылку писем, когда конкретный сценарий c дает сбой (с Mailtrap вместо Gmail)

Я получаю следующую ошибку:

javax.mail.AuthenticationFailedException: 535 5.7.0 Invalid login or password

Я знаю, что пароль и имя пользователя в порядке и пробелов нет.

Это мои зависимости в POM.

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.6.2</version>
    </dependency>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.6.2</version>
    </dependency>

Это мой класс для отправки писем:

    public class SendEmail {

    public static final String SMTP_HOST_NAME = "smtp.mailtrap.io";
    public static final String SMTP_AUTH_USER = "xxxxxxx@xxxxxx.com";
    public static final String SMTP_AUTH_PWD  = "xxxxxxx2019$";
    public static final String SMTP_SF_PORT = "465";
    public static final String SMTP_PORT = "2525";

    public static final String emailMsgTxt      = "Error found while running Test Automation";
    public static final String emailSubjectTxt  = "Error Message From Selenium WebDriver";
    public static final String emailFromAddress = "noreply@mailtrap.io";

    // Add List of Email address where user wish to send the email
    public static final String[] emailList = {"xxxxxxxxx@gmail.com"};

    public void postMail( String recipients[ ], String subject,
                          String message , String from) throws MessagingException
    {
        boolean debug = false;

        //Set the host smtp address
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.socketFactory.port", SMTP_SF_PORT);
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", SMTP_PORT);

        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getDefaultInstance(props, auth);

        session.setDebug(debug);

        // create a message
        Message msg = new MimeMessage(session);

        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);

        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++)
        {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);


        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");

        Transport.send(msg);
        System.out.println("Successfully Sent mail to All Users");
    }

    /**
     * SimpleAuthenticator is used to do simple authentication
     * when the SMTP server requires it.
     */
    private class SMTPAuthenticator extends javax.mail.Authenticator
    {

        public PasswordAuthentication getPasswordAuthentication()
        {
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            return new PasswordAuthentication(username, password);
        }
    }

}

Кто-нибудь может мне помочь, пожалуйста?

1 Ответ

1 голос
/ 23 июня 2020

Неправильные учетные данные! Не используйте учетные данные учетной записи (publi c stati c final String SMTP_AUTH_USER = "xxxxxxx@xxxxxx.com";) вам нужны учетные данные для почтового ящика. enter image description here введите описание изображения здесь

...