Using JavaMail to Search GMail With IMAP
Enable IMAP in GMail Log into your GMail account and go to Settings Switch to tab “Forwarding and POP/IMAP” Select Enable IMAP Set Folder size limits to Limit IMAP folders to contain no more than this many messages 1,000 Less secure app access Go to https://myaccount.google.com/lesssecureapps?pli=1 Turn on Allow less secure apps if this is not turned on, we will get following exception javax.mail.AuthenticationFailedException: [AUTHENTICATIONFAILED] Invalid credentials (Failure) at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:732) at javax.