嗨社区:我尝试在特定脚本失败时自动化电子邮件(使用 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);
}
}
}
有人可以帮我吗?