I'm using a class/method from an old project of mine to send email programmatically. It worked back then, but now I'm getting errors which elude to some kind of gmail security update since I last used this class in the old project:I tried logging in and out through a browser, but that didn't help.
The stack trace shows:
javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs6
534-5.7.14 ZJAPOThluIgJLcM06Bo2G599ldMO1V8IpReZebE8FvDK3uj4KCgtg7CBEUzfjv1ZRjKTY8
534-5.7.14 TnV7OuzgZSBLFq4N89fwfDcYSs1fnsyWLglDAEWbfbltw9eN5aXc0_S_7g8kw0EhLs7IDO
534-5.7.14 mEsQ-FBS3EBd_F78Qv3iOpGeSACqreH_42BZ8XnouZBwfNp02PqLVqcv7wBF_-0YdtXLx4
534-5.7.14 DZb0An9hZCoogawn1j79heQGK4g9w> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 s59sm4398674qtd.20 - gsmtp
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:893)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:814)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:728)
at javax.mail.Service.connect(Service.java:364)
at javax.mail.Service.connect(Service.java:245)
at javax.mail.Service.connect(Service.java:265)
at javax.mail.Transport.send0(Transport.java:251)
at javax.mail.Transport.send(Transport.java:174)
at Emailer.sendEmail(NotificationHandler.java:70)`
.
.
.
and here's my code
class Emailer {
private static String host = "smtp.gmail.com";
private static String user = "[email protected]";
private static String pass = "mypassword";
public static void sendEmail(String fromAddr, String toAddr, String subject, String body) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(fromAddr));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddr));
message.setSubject(subject);
message.setText(body);
Transport.send(message, user, pass);
}
catch (AddressException e) {
e.printStackTrace();
}
catch (MessagingException e) {
e.printStackTrace();
}
}
Possibly because google is blocking your email client.
Google may block sign-in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safe.
I ran into this problem about two months ago, where my Java app cannot send emails with a testing Gmail account. This link explains it in not much detail. But it does offer steps to disable this.