JMeter : Fetching Code From Email

JMeter performance testing is the process of testing the performance of a web application using JMeter. As open-source, Java-based software, JMeter can also be used for functional testing and load testing. When you want to load test user registration scenario where your entered an email ID then a user registration code used to sent to verify the email to complete the user registration process. For such scenario we need to fetch the code from email and pass it following API request .

To fetch the code from email id we need to perform below actions using JMeter:

  1. JMeter connection with email account.
  2. Reading of that particular email from list of emails under inbox.
  3. Fetching of code from email body and storing into JMeter variable.

Lets fetch user registration or email verification code from Gmail account.

1.Setup Gmail Account

Setup gmail account to access by third party application like JMeter. Go to gmail account and click “See all settings”

Enable IMAP by selecting the radio button and click “Save Changes”

Now click your google account

Then click “Manage your Google Account” and click “Security” option on the left and enable/activate 2-step verification

Then click “App Passwords” to setup password. copy and save the generated password

2.Reading Email .

Use eclipse and create a maven project by using below pom xml.

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">

User below code to fetch the email body content with a subject text:

package com.qautomation.utility;

import java.util.Properties;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeBodyPart;

public class EmailUtil {
	public static String getEmailContentBySubject(String UserEmailID,String UserPassword,String EmailSubject)

		String recivingHost = "";
		String contentType = "";
		String messageContent ="";

		Properties props2 = System.getProperties();
		Session session2 = Session.getDefaultInstance(props2,null);
			Store store=session2.getStore("imaps");
			Folder folder =  store.getFolder("INBOX");;
			Message messages[] = folder.getMessages();	
			for(int i=messages.length - 1; i >=0;i--)
				Message message = 	messages[i];
					contentType = messages[i].getContentType();
						Multipart multipart = (Multipart) messages[i].getContent();
						int numberOfParts = multipart.getCount();
						for(int partCount = 0; partCount < numberOfParts; partCount++)
							MimeBodyPart part = (MimeBodyPart)multipart.getBodyPart(partCount);
								messageContent = part.getContent().toString();
					else if(contentType.toLowerCase().contains("text/plain") || contentType.toLowerCase().contains("text/html"))
						Object content = messages[i].getContent();
						if(content != null)
							messageContent = content.toString();
		catch(Exception e)
	return messageContent;


Do a dry run to check if all working fine:

Now create a jar file for the class to be use in JMeter. Right click the class file and select export option.

Select the JAR file option in export wizard

Then select the jar file and location and click finish:

3.Fetching Code using JMeter:

Open JMeter ,create a script and add jar file in test plan

Add a Beanshell sampler in thread group and use below code:

import com.qautomation.utility.EmailUtil;
EmailUtil oEmail = new EmailUtil();
String emailBody = oEmail.getEmailContentBySubject("","<gmail_app_paasword>","User Registration Code");"********************Email Body="+emailBody);

Run the script:

Cheer’s 🙂

Categories: PerformanceTesting

Tagged as:

Leave a Reply