How to access gmail with 2-step verification?

I recently installed 2-step authentication in my gmail account and I am trying to connect to my gmail account using the Java Mail API, but it did not connect.

My code is:

Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); try { Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("imap.gmail.com", " my_account@gmail.com ", "password"); System.out.println(store); Folder inbox = store.getFolder("Inbox"); inbox.open(Folder.READ_ONLY); Message messages[] = inbox.getMessages(); for (Message message : messages) { System.out.println(message); } } catch (NoSuchProviderException e) { e.printStackTrace(); System.exit(1); } catch (MessagingException e) { e.printStackTrace(); System.exit(2); } 

And what I get from logcat:

 javax.mail.AuthenticationFailedException: [ALERT] Application-specific password required: http://support.google.com/accounts/bin/answer.py?answer=185833 (Failure) at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:660) at javax.mail.Service.connect(Service.java:295) at javax.mail.Service.connect(Service.java:176) 

What is the way to overcome this problem, please.

Thanks in advance.

+4
source share
1 answer

When you have activated 2-step verification for your gmail account, log in to the link below using your gmail credentials and create a special password (ASP). You can use the generated password in your code. Just add ASP instead of your regular password. It should work.

https://accounts.google.com/IssuedAuthSubTokens?hide_authsub=1

+4
source

Source: https://habr.com/ru/post/1481305/


All Articles