英文:
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.
<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>
This is my class for sending emails:
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);
}
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论