Confirm email received

Confirm email received

I have to confirm that an email was sent after specific events.

I can browse to gmail and fetch that way, but would prefer to use imap or rest api to do that.

Any help or direction will be appreciated

Mailinator would delete emails for every 30 min or 120min. That should not be a problem

Thank you.

Unfortunately it might contain sensitve info.

javax.mail works like a charm.

Automating GMAIL is not a good idea. GMAIL has updated their privacy policy. But still quite many users automate to retrieve mails. You may do it using Java mailx - Imap. In Katalon we need to add it as a custom keyword.

Suggested approach would be using Mailinators unless the mail contains sensitive data. I usually use Mailinator for forgot passwords and to test notifications.

You can use gmail for testing purposes. You can use gmail account aliases to send unlimited emails to your gmail account. You can create a gmail account only for testing and then use it for all your testing. For example let me share with you this link

Also I have setup a video tutorial just for this email confirmation automation https://www.youtube.com/watch?v=9G0S3O9HZWQ

1 Like

@“Muhammad Asif” - excellent tutorial. I followed your example, changing some global variable names / values to match what I need, but I keep getting this error:

No signature of method: ConfirmEmail.fetchEmailVerifyURL() is applicable for argument types: () values: []

nothing in the code base has changed

How do you pass parameters for that function? That error message indicates you pass wrong parameter type into that function

The variables are global (per the tutorial), but I changed their names and values to fit what I needed. Will revisit and make corrections, while I wait for your reply :slight_smile:

The topic is closed due to inactiveness. Feel free to create a new one if you all still have any concerns.

I want to automate the email validation flow using Katalon. I donot want to use gmail. Can we access outlook through web and then validate email. Currently, when I am trying to do so, I get an authentication popup. Can anyone suggest how to handle authentication popup using Katalon?

Don´t know why but I have the same issue. I have editted the script a little to fit to what I need to do and pass the relevant global variables when I call the keyword. If it helps take a look to the changes I made:

(Where says “docusign?token” at the bottom you have to modified with word that are contained in the URL your want to open. In my case is allways the same)

package keywordUtils

import javax.mail.Folder
import javax.mail.Message
import javax.mail.MessagingException
import javax.mail.NoSuchProviderException
import javax.mail.Session
import javax.mail.Store
import javax.mail.Message.RecipientType
import javax.mail.search.AndTerm
import javax.mail.search.RecipientStringTerm
import javax.mail.search.SearchTerm
import javax.mail.search.SubjectTerm

import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import internal.GlobalVariable

public class ConfirmEmail {
	
	@Keyword
	def fetchEmailVerifyURL(String QAEmail, String QAPassword, String EmailRecipient) {

		String url = findConfirmEmailURL(QAEmail, QAPassword, EmailRecipient)
		WebUI.navigateToUrl(url)								// Navigate to the URL retrieved
	}

	// Mail related methods
	private String findConfirmEmailURL(String QAEmail, String QAPassword, String EmailRecipient) throws MessagingException {

		SearchTerm filter = createFilter(EmailRecipient);						// 1. Create mail search filter for our mailbox
		Store store = connect(QAEmail, QAPassword);								// 2. Connect to our mailbox
		String content = getMessageContent(store, filter);		// 3. Retrieve the message body from the received email
		String url = getURL(content);							// 4. Get the confirmation URL from the message body
		return url;
	}


	private SearchTerm createFilter(String Recipient){
		SearchTerm t1 = new RecipientStringTerm(RecipientType.TO, Recipient);
		SearchTerm t2 = new SubjectTerm("Welcome to LemonBrew");
		SearchTerm st = new AndTerm(t1, t2);
		return st;
	}

	private Store connect(String QAEmail, String QAPassword) throws MessagingException {

		Properties props = new Properties();
		props.setProperty("mail.store.protocol", "imaps");

		Session session = Session.getDefaultInstance(props, null);
		Store store;

		try{

			store = session.getStore("imaps");
			store.connect("imap.gmail.com", QAEmail, QAPassword);

		}catch (NoSuchProviderException e) {

			e.printStackTrace();
			throw e;
		}catch (MessagingException e) {

			e.printStackTrace();
			throw e;
		}

		return store;
	}

	public String getMessageContent(Store store, SearchTerm filter) throws MessagingException {

		String mailMessageContent = "";
		try{

			Folder emailFolder = store.getFolder("INBOX");
			emailFolder.open(Folder.READ_ONLY);

			Message[] messages = emailFolder.search(filter);
			System.out.println("messages.length---" + messages.length);

			if(messages.length != 0){

				Message message = messages[0];

				String subject = message.getSubject();
				String from = message.getFrom()[0].toString();
				String content = message.getContent().toString();

				mailMessageContent = content;
			}

			emailFolder.close(false);
		} catch (MessagingException e) {

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

			e.printStackTrace();
		}finally {

			store.close();
		}

		return mailMessageContent;
	}

	private String getURL(String content){
		Document doc = Jsoup.parse(content);
		//Element element = doc.selectFirst(GlobalVariable.G_Confirm_Url_Search_Expr);
		Element element = doc.getElementsByAttributeValueContaining("href", "docusign?token")[0]
		return element.attr("href");
	}
}

@Muhammad_Asif Excelent work you made here mate. I was searching this solution for a while! Keep rocking

I just got your email. I actually forgot about this tutorial sharing here. Thanks for appreciation.

For the previous messages sorry that I perhaps missed them in the email or may be never got them.