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.
source share