title: How parse email and get hyperlink from mail JavaMail?
body: <p>After spending a lot of time in finding a solution to their problem and I have not found a solution. In general, the problem is as follows:
  There is a web application where you can send an invitation to work on the project, the standard form filling data including e-mail which will be sent. And so the task to cover the auto test this functionality. Initially I thought to do it all on selenium'e after sending an invitation to open a new instanst browser, go to the postal service which will be sent an invitation and there manipulate the search and the discovery of the letter, invite, and pressing the button. I understand that it's a bit tugovato process and found a solution to their problem is to use the JavaMail library to receive messages. Ie to connect to the mail service in this example I use Gmail, and from there get all the letters.</p>
  
  <pre><code>import java.util.Properties;
  
  public class PropertiesEmail {
  
  String host = "imap.gmail.com";
  String user = "userEmail";
  String password = "password";
  int port = 993;
  
  public Properties setServerProperties(){
      Properties properties = new Properties();
      properties.put("mail.imap.host", host);
      properties.put("mail.imap.port", port);
      properties.put("mail.imap.starttls.enable", "true");
      properties.put("mail.store.protocol", "imaps");
      return properties;
  }
  
  }
  
  import javax.mail.*;
  import javax.mail.search.FlagTerm;
  import java.util.Properties;
  
  public class CheckUnreadEmail {
  
  public static void checkUnreadEmail(){
  
      try{
  
          //Create object email properties
          PropertiesEmail propertiesEmail = new PropertiesEmail();
  
          //Set email server properties
          Properties props = propertiesEmail.setServerProperties();
  
          Session session = Session.getDefaultInstance(props);
  
          Store store = session.getStore();
  
          store.connect(propertiesEmail.host, propertiesEmail.user, propertiesEmail.password);
  
          //Create the folder object and open it
          Folder folder = store.getFolder("INBOX");
          folder.open(Folder.READ_WRITE);
  
          //Total unread messages
          System.out.println("Total messages: " + folder.getMessageCount());
          System.out.println("Unread messages: " + folder.getUnreadMessageCount());
  
  
          //Create variable for search unread message
          FlagTerm flag = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
  
          //Retrieve all messages
          //Message [] messages = folder.getMessages();
  
          //Retrieve unread messages from the folder INBOX
          Message [] unreadMessage = folder.search(flag);
  
          for (int i = 0, n = unreadMessage.length; i &lt; n; i++){
              Message message = unreadMessage[i];
              System.out.println("--------------");
              System.out.println("Subject: " + message.getSubject());
          }
  
          //folder.setFlags(unreadMessage, new Flags(Flags.Flag.SEEN), true);
  
          //close the store and folder objects
          folder.close(false);
          store.close();
  
      }catch(Exception e){
          e.printStackTrace();
      }
  
  }
  
  }
  </code></pre>
  
  <p>There are a few questions:
  1. How to wait for a new letter in the mail? (Since after sending the email invitation does not come at once)
  2. How to parse received the letter and get a hyperlink to an invite?</p>