How to get email content/body using Katalon?

Hi,

I’m looking for a way to get the content/body of a mail using Katalon. I’ve tried using the Gmail plugin which is available in the store but it looks like it needs an update (couldn’t get it to work properly due to several errors).

Is it even possible to use Gmail in this case (as they seem to have a strict privacy policy now)?

I’d appreciate any help.

Thanks in advance.

You can import and use the JavaMail package. I have tested it out enough to know it will work for us when we get to the point of needing it:

https://www.tutorialspoint.com/javamail_api/javamail_api_quick_guide.htm

Though you will need to go in to the security settings for the account and allow ‘Less secure app access’, otherwise any calls to authenticate will be blocked.

Thank you, I’ll give that a try!

1 Like

Here you go, give this a try.

Obviously use your own mailbox username/pw combo: copy the text and dump it into your own keywords package/class etc.

The getLastReceivedEmailBody function has a folder.getMessages() call. That returns them in reverse order of when they were received, so newest is last.

JavaMail.txt (7.7 KB)

1 Like

Thank you! this seems to be working much better than the plugin I tried before.

1 Like

Hello again,

it’s working perfectly fine with one exception: If I try to get the newest mail body, it doesn’t return the text for some reason.

This is how the code looks after I added the “newest email” part:
image

If I use getContent().toString on foundMessages[foundMessages.length-1] it returns the HTML code of my email but it doesn’t seem to work with your getTextFromMessage() function. Maybe I missed something here.

Hi,

That is interesting. I just tried it again here and it does work OK.

Thinking it could be something to do with the format of the email perhaps…

I can’t look in to it any deeper right now due to other work stuff but will try and have a look over the weekend.

Hey,

no worries, take your time. I believe you may be right. I tried it with a different mailbox and it worked fine. I’ll post here if I find a solution.

I managed to solve my problem by basically converting the HTML mail into text. Thanks again for your help.

1 Like

Hi, glad you worked it out.

I expect I will need it to work with HTML for our project so will post back here when I have it sorted.

1 Like

This might be a dead thread by now, but I have created a keyword from the javamail.txt and am struggling with what to call as my keyword. I have:

package global

public class EmailHelpers {

	private Folder folder;

	public enum EmailFolder {
		INBOX("INBOX"),
		SPAM("SPAM");

		private String text;

		private EmailFolder(String text){
			this.text = text;
		}

		public String getText() {
			return text;
		}
	}

	/**
	 * default constructor to get the connection to our email inbox
	 */
	public EmailHelpers() {
		// connect to our qa test email inbox
		connectToInbox(GlobalVariable.automationEmail, GlobalVariable.automationPassword, EmailFolder.INBOX)
	}

…and then the rest from the txt above.

I am trying to figure out what I need to call in order to connect and pull messages. I feel ridiculous as I have worked with keywords hundreds of times, but for some reason I am not figuring this out.

I am calling it: CustomKeywords.‘global.EmailHelpers.connectToInbox’(‘emailaddress@gmail.com’, ‘xxxxxxxxxxxxx’, ‘INBOX’)

Perhaps the following applies;

Google Gmail is highly security conscious. It rejects “third-party apps”, which includes all apps that work on Selenium WebDriver, which includes Katalon Studio.

However this page tells that you can try something:

When a third-party app meets our security standards, you can:

Afaik connecting to a gmail inbox does not work anymore (see kazurayams reply). Right now I’m still using a modified version of this class to connect to an outlook inbox.

You need to create a new instance of the EmailHelpers class first. The constructor calls the connectToInbox method. Afterwards you can use any of the other methods to do whatever you like.

Here’s an example:

EmailHelpers eh = new EmailHelpers('INBOX')

println eh.getNewestMail()