How to capture link from email as a variable

So a fellow manager and I spent 4 hours working on this and finally came up with the solution together. He doesn’t really do java and never seen Katalon till I showed it to him. So we put our heads together and hit the web and used this as a starting point:

Here is how I made it work in Katalon, with outlook and with returning a URL I can navigate to:

First I created a package and a class under keywords. I import the package and class into my test when I use it.

Here is the code I have in the class:

package emailVerification

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.jsoup.Jsoup as Jsoup;

 * from
 * Author(s): B_L
 * Created: 01/30/2019
 * Last Modified by: B_L
 * Last Modified: 01/30/2019
 * Purpose: Grab verify link from email
public class FetchEmailVarLink {

	public static check(String host, String storeType, String user,
			String password) {
		try {
			String emailLink = ''
			//create properties field
			Properties properties = new Properties();

			properties.put("mail.imap.port", "993");
			properties.put("mail.imap.starttls.enable", "true");
			properties.setProperty("mail.imap.socketFactory.fallback", "false");
			Session emailSession = Session.getDefaultInstance(properties);

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

			store.connect(host, user, password);

			//create the folder object and open it
			Folder emailFolder = store.getFolder("INBOX");;

			// retrieve the messages from the folder in an array and print it
			MimeMessage[] messages = emailFolder.getMessages();
			//System.out.println("messages.length---" + messages.length);
			int n=messages.length;

			//change n- to number of emails you want to dig through
			for (int i = n-5; i<n; i++) {
				MimeMessage message = messages[i];
				ArrayList<String> links = new ArrayList<String>();

				if(message.getSubject().contains("Verify your email for project") || message.getSubject().contains("Sign in to project")){
					//System.out.println("Subject: " + message.getSubject());
					MimeMultipart messageBody = message.getContent();

					String desc = messageBody.getBodyPart(1).getContent().toString();

					//System.out.println("Description: " + desc);
					Pattern linkPattern = Pattern.compile("href=\"(.*?)\"",  Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
					Matcher pageMatcher = linkPattern.matcher(desc);
					System.out.println("Email:"+ i + " is not a wanted email");
				for(String temp:links){
						emailLink = temp;

			emailLink = Jsoup.parse((emailLink).toString()).text();
			return emailLink;

			//close the store and folder objects

		} catch (NoSuchProviderException e) {
		} catch (MessagingException e) {
		} catch (Exception e) {


	public static String getEmailLinkFromEmail(){
		String host = "";
		String mailStoreType = "imap";
		String username = "yourEmail";
		String password = "yourPassword";

		String emailLink = check(host, mailStoreType, username, password);
		return emailLink;


short example:

import emailVerification.FetchEmailVarLink

String emailLink = FetchEmailVarLink.getEmailLinkFromEmail()
WebUI.navigateToUrl(emailLink, FailureHandling.CONTINUE_ON_FAILURE)

Original conversation: