I am getting this error when I try to send mail using the JavaMail API:
javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accept
mail.smtp.host=smtp.gmail.com
,
mail.smtp.port=25
,
mail.smtp.auth=true
mail.smtp.starttls.enable=true
Now,make sure that two step verification is off
Allow less secure app (ON) follow this link :-
https://myaccount.google.com/lesssecureapps
Sorry for coming late to Party.These could be the problem in your task if you are using Gmail Server.
You probably got this error because the username and password of the from mail id is not matching. Please recheck your password and mail-id (username). It could be a typo.
In some cases, Gmail prevents logging in through external applications or programs which are not authorised. Also login to your gmail account to check if gmail has prevented logging in to your account via your Java Mail API program.
If nothing works, you could try some other SMTP server (like yahoo, yandex).
Step 1: Log into your gmail account
Step 2: Click Settings
Step 3: Click the Accounts and Import Tab > Other Google Account Settings
Step 4: Click Security
Step 5: Set Allow less secure apps to ON
Follow the steps:
1.Allow Less Secure App(should be turned on).
2.Check Gmail Username and Password..
public static void main(String[] args) {
final String username = "YourMailId";
final String password = "password";
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true"); //TLS
Session session = Session.getInstance(prop,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("Tarunsunny143@gmail.com"));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse("balachandralucky2@gmail.com, to_username_b@yahoo.com")
);
message.setSubject("Testing Gmail TLS");
message.setText("Dear Mail Crawler,"
+ "\n\n Please do not spam my email!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}