The code I'm using to send a simple mail:
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);
}
}
}
When I run the above code I got following exception, user name that I provide and the password that I provided are correct. I also tried giving just the username without adding @gmail.com still it doesn't work.
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)