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("mail.store.protocol", "imaps");
           Session session = Session.getDefaultInstance(props, null);
           store = session.getStore("imaps");
           //create properties field
          /* Properties properties = new Properties();

           properties.put("mail.pop3.host", "pop.gmail.com");
           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("imap.gmail.com", "qatechtools@gmail.com","Yourpassword");
     
           Folder folder = store.getFolder("INBOX");
           folder.open(Folder.READ_WRITE);

           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 = folder.search(new FromStringTerm("Your Search Filter")
               folder.getMessages());

               System.out.println(messages.length);
               //Wait for 10 seconds
               if (messages.length == 0) {
                   Thread.sleep(1000);
               }
               else{
                   break;
               }
           }

           //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
                               .getInputStream()));
               while ((line = reader.readLine()) != null) {
                   buffer.append(line);
               }
               System.out.println(buffer);

               //Your logic to split the message and get the Registration URL goes here
                messageURL = buffer.toString().split(
                       "<")[1].split(">")[0]
                       ;
               System.out.println(messageURL);
               return messageURL;
           }
       }