title: How to refactor two similar methods?
body: <p>I have two methods that wait for an email to arrive and retrieve emailID. They are static. Is it possible to do these methods better? Because many parts of these methods are the same.</p>
  <pre><code>     public static String waitForExcaEmailId(String fieldName, String subject)  throws GeneralSecurityException, IOException {
          String emailID = &quot;&quot;; int i = 0;
          do{
              Thread.sleep(5000);
              emailID = getFakeMailID(getMessagesList(fieldName, url), MAIL_FROM, getCurrentDate(), subject);
              i++;
              logInfo(&quot;Mail ID: &quot; + emailID);
              if(i &gt; TIMEOUT_MAX_TIME_WAITING_FOR_MAIL_APPEARANCE) throw new RuntimeException(&quot;Email ID not found&quot;);
          }while(emailID == null)
  
          return emailID;
      }
  
      public static String waitForGoogleEmailID(String fieldName, String subject) throws GeneralSecurityException, IOException {
          String emailID = &quot;&quot;; int i = 0;
          do{
              Thread.sleep(5000);
              emailID = getGoogleMailID(MAIL_FROM, getCurrentDate(), subject);
              i++;
              logInfo(&quot;Mail ID: &quot; + emailID);
              if(i &gt; TIMEOUT_MAX_TIME_WAITING_FOR_MAIL_APPEARANCE) throw new RuntimeException(&quot;Email ID not found&quot;);
          }while(emailID == null);
  
          return emailID;
      }    
  </code></pre>