我用来发送简单邮件的代码:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
public class SendMailUsingAuthentication
{
public static final String SMTP_HOST_NAME = "smtp.gmail.com";
public static final String SMTP_AUTH_USER = "username@gmail.com";
public static final String SMTP_AUTH_PWD = "password";
public static final String emailMsgTxt ="error found wgile clicking on an element";
public static final String emailSubjectTxt ="Test Email";
public static final String emailFromAddress ="hiphop.sujai@gmail.com";
public static final String[] emailList = {"receiver.username@gmail.com"};
public void postMail(String recipients [], String subject, String message, String from) throws MessagingException
{
boolean debug = false;
Properties props = new Properties();
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
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("Successfully sent mail to all users");
}
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);
}
}
}
当我运行上面的代码时,我得到以下异常,我提供的用户名和我提供的密码是正确的。我也尝试过只提供用户名而不添加@ gmail.com,但它还没有用。
Exception in thread "main" javax.mail.AuthenticationFailedException: 534-5.7.14
<https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvTi
534-5.7.14 QLG6LXdGFhnbFAdBRTtZO4ynMurcJa7KsqyOjGLcdS9lAyFP42I6l75P9B_90NPxQr-Bj-
534-5.7.14 0Yzk9vWoigAtOgTD3_D4vp4fEJ7C8oCtFTj4kRJfRJwJJvgPzdZ7ujc1zNyksYMYhLBaIP
534-5.7.14 LpYpcxJKLz90-rBx5NdWeJTu_1U1FENAX4QxVjsYaeeuJxMjfk6c1aZBMm2e485o3PV2H4
534-5.7.14 9rQ-6HA> Please log in via your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754
om7sm11753908pdb.61 - gsmtp
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate
(SMTPTransport.java:648)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:583)
at javax.mail.Service.connect(Service.java:313)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:87)
at Capture_Screen.main(Capture_Screen.java:32)
答案 0 :(得分:5)
我遇到了同样的问题。事实证明,谷歌阻止了访问邮件帐户的可疑尝试。我刚刚在浏览器中登录到gmail,窗口顶部出现错误/警告消息,提醒我有可疑活动。点击消息后,我能够允许来自“可疑”主机的连接,这确实是我在AWS上运行的应用程序。
答案 1 :(得分:-1)
这背后的原因是Gmail检测到安全性较低的身份验证方法,因此会出现身份验证错误。他们这样做是为了您的安全。
您可以使用更安全的身份验证,例如OAuth 2.0。
还有一种方法可以实现安全性较低的身份验证。它可以在您的Gmail帐户的安全设置下启用。