How to login in gmail using selenium bypassing the security

Many people are facing the challenge of login in to a google account after Google has disabled login through automation tools or code. One simple way to bypass the security is by using the Java mail API.

Following the code will help you achieve the same. The below code returns any URL from the body of the mail received and returns it as a string.

public String  readGmail() throws Exception {
           Store store = null;

           Properties props = System.getProperties();
           props.setProperty("", "imaps");
           Session session = Session.getDefaultInstance(props, null);
           store = session.getStore("imaps");
           //create properties field
          /* Properties properties = new Properties();

           properties.put("", "");
           properties.put("mail.pop3.port", "995");
           properties.put("mail.pop3.starttls.enable", "true");
           Session emailSession = Session.getDefaultInstance(properties);

           //create the POP3 store object and connect with the pop server
           Store store = emailSession.getStore("pop3s");*/

           store.connect("", "","Yourpassword");
           Folder folder = store.getFolder("INBOX");

           System.out.println("Total Message:" + folder.getMessageCount());
           System.out.println("Unread Message:"
                   + folder.getUnreadMessageCount());

           Message[] messages = null;
           boolean isMailFound = false;
           Message mailFromGmail= null;

           //Search for mail from outplay
           for (int i = 0; i <5; i++) {
               messages = FromStringTerm("Your Search Filter")

               //Wait for 10 seconds
               if (messages.length == 0) {

           //Search for unread mail from God
           //This is to avoid using the mail for which
           //Registration is already done
           for (Message mail : messages) {
               if (!mail.isSet(Flags.Flag.SEEN)) {
                   mailFromGmail = mail;
                   System.out.println("Message Count is: "
                           + mailFromGmail.getMessageNumber());
                   isMailFound = true;

           //Test fails if no unread mail was found from Gmail
           if (!isMailFound) {
               throw new Exception(
                       "Could not find new mail from Gmail :-(");

               //Read the content of mail and launch registration URL
           } else {
               String line;
               StringBuffer buffer = new StringBuffer();
               BufferedReader reader = new BufferedReader(
                       new InputStreamReader(mailFromGmail
               while ((line = reader.readLine()) != null) {

               //Your logic to split the message and get the Registration URL goes here
                messageURL = buffer.toString().split(
               return messageURL;