使用Mailtrap发送邮件当自动化脚本失败(Selenium – Java)

huangapple 未分类评论49阅读模式
英文:

Sending Emails with Mailtrap When automated script fails (Selenium - Java)

问题

嗨,社区:我尝试在特定脚本失败时自动发送电子邮件(使用Mailtrap而不是Gmail)。

我收到以下错误消息:

javax.mail.AuthenticationFailedException: 535 5.7.0 无效的登录或密码

我知道密码和用户名都是正确的,而且没有空格。

这是我的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      = "运行测试自动化时发现错误";
    public static final String emailSubjectTxt  = "来自Selenium WebDriver的错误消息";
    public static final String emailFromAddress = "noreply@mailtrap.io";

    // 添加用户希望发送电子邮件的电子邮件地址列表
    public static final String[] emailList = {"xxxxxxxxx@gmail.com"};

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

        // 设置主机smtp地址
        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);

        // 创建一条消息
        Message msg = new MimeMessage(session);

        // 设置发件人和收件人地址
        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);

        // 设置主题和内容类型
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");

        Transport.send(msg);
        System.out.println("成功发送邮件给所有用户");
    }

    /**
     * SMTPAuthenticator用于在SMTP服务器需要时进行简单身份验证。
     */
    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);
        }
    }
}

有人能帮我吗?请。

英文:

Hi community: I try to automate Emails when a specific script fails (With Mailtrap instead of Gmail)

I'm getting the next error below:

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

I know that the password and the username are OK and there are no spaces.

These are my dependencies in POM.

    &lt;dependency&gt;
        &lt;groupId&gt;javax.mail&lt;/groupId&gt;
        &lt;artifactId&gt;javax.mail-api&lt;/artifactId&gt;
        &lt;version&gt;1.6.2&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.sun.mail&lt;/groupId&gt;
        &lt;artifactId&gt;javax.mail&lt;/artifactId&gt;
        &lt;version&gt;1.6.2&lt;/version&gt;
    &lt;/dependency&gt;

This is my class for sending emails:

    public class SendEmail {

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

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

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

    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(&quot;mail.smtp.host&quot;, SMTP_HOST_NAME);
        props.put(&quot;mail.smtp.socketFactory.port&quot;, SMTP_SF_PORT);
        props.put(&quot;mail.smtp.socketFactory.class&quot;,
                &quot;javax.net.ssl.SSLSocketFactory&quot;);
        props.put(&quot;mail.smtp.starttls.enable&quot;, &quot;true&quot;);
        props.put(&quot;mail.smtp.auth&quot;, &quot;true&quot;);
        props.put(&quot;mail.smtp.port&quot;, 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 &lt; 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, &quot;text/plain&quot;);

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

    /**
     * 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);
        }
    }

}

Can anybody help me, please?

答案1

得分: 1

Credentials are wrong!
Don't use the account credentials (
public static final String SMTP_AUTH_USER = "xxxxxxx@xxxxxx.com";) you need the inbox credentials.
使用Mailtrap发送邮件当自动化脚本失败(Selenium – Java)
使用Mailtrap发送邮件当自动化脚本失败(Selenium – Java)

英文:

Credentials are wrong!
Don't use the account credentials (
public static final String SMTP_AUTH_USER = "xxxxxxx@xxxxxx.com";) you need the inbox credentials.
使用Mailtrap发送邮件当自动化脚本失败(Selenium – Java)
使用Mailtrap发送邮件当自动化脚本失败(Selenium – Java)

huangapple
  • 本文由 发表于 2020年5月29日 06:06:18
  • 转载请务必保留本文链接:https://java.coder-hub.com/62075328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定