Source:
public static String getFirstEmailContent(String userName, String password, String subject) { try { PageObjectLogging.logInfo("Checking emails for " + userName + " that contain '" + subject + "'"); Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("imap.googlemail.com", userName, password); Folder inbox = store.getFolder("Inbox"); inbox.open(Folder.READ_ONLY); Message[] messages = null; boolean forgottenPasswordMessageFound = false; Message magicMessage = null; for (int i = 0; !forgottenPasswordMessageFound; i++) { messages = inbox.getMessages(); PageObjectLogging.log("Mail", "Waiting for the message", true); Thread.sleep(2000); for (Message message : messages) { if (message.getSubject().contains(subject)) { forgottenPasswordMessageFound = true; magicMessage = message; } } if (i > 15) { throw new WebDriverException("Mail timeout exceeded"); } } PageObjectLogging.log("Mail", "Mail arrived", true); Message m = magicMessage; String line; StringBuilder builder = new StringBuilder(); InputStreamReader in = new InputStreamReader(m.getInputStream()); BufferedReader reader = new BufferedReader(in); while ((line = reader.readLine()) != null) { builder.append(line); } store.close(); return builder.toString(); } catch (NoSuchProviderException e) { PageObjectLogging.log("getFirstEmailContent", e, false); throw new WebDriverException(); } catch (MessagingException | IOException | InterruptedException e) { PageObjectLogging.log("getFirstEmailContent", e, false); PageObjectLogging.log("Cannot log in", String.format("cannot log in using credentials: %s/%s", userName, password), false); throw new WebDriverException(); } }

Target:
public static String getFirstEmailContent(String userName, String password, String subject) { try { PageObjectLogging.logInfo("Checking emails for " + userName + " that contain '" + subject + "'"); Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("imap.googlemail.com", userName, password); Folder inbox = store.getFolder("Inbox"); inbox.open(Folder.READ_ONLY); Message[] messages = null; boolean forgottenPasswordMessageFound = false; Message magicMessage = null; for (int i = 0; !forgottenPasswordMessageFound; i++) { messages = inbox.getMessages(); PageObjectLogging.log("Mail", "Waiting for the message", true); Thread.sleep(2000); for (Message message : messages) { if (message.getSubject().contains(subject)) { forgottenPasswordMessageFound = true; magicMessage = message; } } if (i > 15) { throw new WebDriverException("Mail timeout exceeded"); } } PageObjectLogging.log("Mail", "Mail arrived", true); Message m = magicMessage; String line; StringBuilder builder = new StringBuilder(); InputStreamReader in = new InputStreamReader(m.getInputStream()); BufferedReader reader = new BufferedReader(in); while ((line = reader.readLine()) != null) { builder.append(line); } store.close(); return builder.toString(); } catch (NoSuchProviderException e) { PageObjectLogging.log("getFirstEmailContent", e, false); throw new WebDriverException(); } catch (MessagingException | IOException | InterruptedException e) { PageObjectLogging.log("getFirstEmailContent", e, false); throw new WebDriverException(); } }