javax.mail.AuthenticationFailedException is thrown while sending email in java
Solution 1
I had the same problem! Google prevents access for less secure apps.
This is how I solved it:
- Log in from your browser to that email account.
- Go to https://www.google.com/settings/security/lesssecureapps
- You will see Turn off or Turn on. Click on Turn on, then try your code again. It should work now.
Solution 2
- Make sure Less secure apps is TURNED ON
https://www.google.com/settings/security/lesssecureapps
- Allow each app to send email
Go to https://accounts.google.com/b/0/DisplayUnlockCaptcha
and click on Continue.
- This time: you can use your app to send email and all operations are allowed.
More links:
https://support.google.com/accounts/answer/6010255
https://productforums.google.com/forum/#!topic/gmail/9KCgzXY4G_c
Solution 3
- allow less secure apps from your google account settings https://www.google.com/settings/security/lesssecureapps
- open your gmail> goto settings > IMAP/POP settings > enable IMAP
- goto this url and turn this on (completing all these steps will surely work) https://www.google.com/accounts/DisplayUnlockCaptcha
official guide for help
https://support.google.com/mail/answer/7126229?hl=en-GB&visit_id=637246238859828954-474780158&rd=2
Solution 4
I am also facing same problem.
Google does't give you direct open port access. If you are using your your google account for mail sending please Turn on the setting by clicking on this link. google go here : https://www.google.com/settings/security/lesssecureapps Best of luck hope it will work for you as well.
Mahira Khan
Updated on July 05, 2022Comments
-
Mahira Khan almost 2 years
I am beginner in java and I want to send an email in java, for that I am using this code in Java. But my code is throwing an exception, and I need a heads-up why…
This is stack trace of exception:
javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbsNX 534-5.7.14 No6jJbDc4l7fZ_WLdBD0sNHIIp_nLvplRMm0bYFBnZBF_XOyVvNSdd1FenDZJPwBTFQyRH 534-5.7.14 lriPK3myMm-dXkW3zK0-6XpO7BzI8hfRcByG1k7YiVzXlddTvs7QhjtgCWNcrzMBuPhoof 534-5.7.14 GjME2TgYzXJVHz5MV98nRnr_kq-kP7RmgOtX3IQHLwM5E8QGBC9-2THVQr_Ch_U0-1nZsc 534-5.7.14 yoPuNEw> 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 wr6sm26888533wjc.24 - gsmtp at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:892) 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 SendEmail.sendFromGMail(SendEmail.java:50) at SendEmail.main(SendEmail.java:18) sent
This is my code
import java.util.*; import javax.mail.*; import javax.mail.internet.*; public class SendEmail { private static String USER_NAME = "me"; private static String PASSWORD = "xyz"; private static String RECIPIENT = "[email protected]"; public static void main(String[] args) { String from = USER_NAME; String pass = PASSWORD; String[] to = { RECIPIENT }; String subject = "Java send mail example"; String body = "Welcome to JavaMail!"; sendFromGMail(from, pass, to, subject, body); System.out.println("sent"); } private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) { Properties props = System.getProperties(); String host = "smtp.gmail.com"; props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.user", from); props.put("mail.smtp.password", pass); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props); MimeMessage message = new MimeMessage(session); try { message.setFrom(new InternetAddress(from)); InternetAddress[] toAddress = new InternetAddress[to.length]; for( int i = 0; i < to.length; i++ ) { toAddress[i] = new InternetAddress(to[i]); } for( int i = 0; i < toAddress.length; i++) { message.addRecipient(Message.RecipientType.TO, toAddress[i]); } message.setSubject(subject); message.setText(body); Transport transport = session.getTransport("smtp"); transport.connect(host, from, pass); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (AddressException ae) { ae.printStackTrace(); } catch (MessagingException me) { me.printStackTrace(); } } }
-
Buffalo over 9 yearsIt's now a checkbox and you should click "TURN ON". Please update your answer.
-
Plain_Dude_Sleeping_Alone almost 9 yearsI wish I could upvote this answer multiple times, Thank you so much.
-
Santhosh R over 8 yearsIt works. but it is not the most elegant way of doing it. Because it leaves your mail account vulnerable as you are compromising on security
-
biniam over 8 years@Adelin If you follow this steps properly, it should work. If you find a better solution, please edit this answer or add a link to your answer.
-
Admin over 8 yearsthanks brother, @biniam_Ethiopia it works for me. love you
-
Md Sufi Khan over 8 yearsWorking fine. You saved my time. Thanks :)
-
saurabheights almost 8 yearsMajor +1. The second DisplayUnlockCaptcha was necessary. I had three servers, two of which worked fine after lesssceureapps link, but the third one worked using DisplayUnlockCaptch. Thanks biniam_Ethiopa, take my vote.
-
Yster almost 8 yearsI you use the new gmail api, you don't have to enable less secure apps: developers.google.com/gmail/api/quickstart/java
-
Alex Rogachevsky over 7 yearsDoes the trick for AWS EC2. I suspect Google devs will add more flags like that in the future to curb spammers. Typical "bug fixing" by some overworked developer.
-
visrahane almost 7 yearsFor all those this didn't work, wait for some 5 mins. For me it took some time.
-
Deepak over 6 yearsDidn't work for me. I changed it to less secure app also.
-
pheromix about 6 yearsThank you very much @biniam_Ethiopia !
-
Pratik Patel almost 6 yearsThank you, Your solution is perfect.!
-
Zaid about 5 yearsI saw that a lot of posts mention the first step but the second link is what made it really work for me...thankyou.
-
Sandip Solanki over 4 yearsI already have the less secure app TURNED ON but still I am getting this exception when I try to send email from Google VM Instance. However it works find in my local environment. Please guide.