How to capture link from email as a variable


#1

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:
https://stackoverflow.com/questions/36707939/how-to-extract-a-registration-url-from-a-mail-content

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 https://stackoverflow.com/questions/36707939/how-to-extract-a-registration-url-from-a-mail-content
 * 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.host",host);
			properties.put("mail.imap.port", "993");
			properties.put("mail.imap.starttls.enable", "true");
			properties.setProperty("mail.imap.socketFactory.class","javax.net.ssl.SSLSocketFactory");
			properties.setProperty("mail.imap.socketFactory.fallback", "false");
			properties.setProperty("mail.imap.socketFactory.port",String.valueOf(993));
			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");
			emailFolder.open(Folder.READ_ONLY);

			// 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);
					while(pageMatcher.find()){
						links.add(pageMatcher.group(1));
					}
				}else{
					System.out.println("Email:"+ i + " is not a wanted email");
				}
				for(String temp:links){
					if(temp.contains("emailSubjectHere")){
						emailLink = temp;
						//System.out.println(temp);
					}
				}


			}
			emailLink = Jsoup.parse((emailLink).toString()).text();
			System.out.println(emailLink);
			return emailLink;

			//close the store and folder objects
			emailFolder.close(false);
			store.close();

		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}


	public static String getEmailLinkFromEmail(){
		String host = "outlook.office365.com";
		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.comment(emailLink)
WebUI.navigateToUrl(emailLink, FailureHandling.CONTINUE_ON_FAILURE)

Original conversation: https://forum.katalon.com/t/receive-an-email-and-click-a-link/17353/13