1

I have a Jmeter script which follows the following steps 1. registers user 2. Reads email using Mail Reader Sampler which has following script

StringBuilder aggregateResult = new StringBuilder()
prev.getSubResults().each {
    aggregateResult.append(it.getResponseDataAsString())
    it.getSubResults().each {
        aggregateResult.append(it.getResponseDataAsString())
        it.getSubResults().each {
            aggregateResult.append(it.getResponseDataAsString())        
        }   
    }
}
prev.setResponseData(aggregateResult.toString().getBytes())
  1. Then extracts a particular link based on regexp.

As of now, it reads either the latest email or all the unread emails in the server. Can someone please help me to modify the above script to read the message based on the user email created at step 1? Emails are created like test+1@gmail.com, test+2@gmail.com and so on.

Manoj Kengudelu
  • 647
  • 1
  • 8
  • 21

2 Answers2

2

Unfortunately it is not something you can do with Mail Reader Sampler, if you need to get mail(s) only for this or that sender email address you can use JavaMail API which provides filtering using i.e. FromStringTerm class from JSR223 Sampler

Example code:

import javax.mail.Multipart

Properties properties = new Properties()
properties.put('mail.imap.host', 'your mail server host') // i.e. imap.gmail.com
properties.put('mail.imap.port', your mail server port)  // i.e. 993
properties.setProperty('mail.imap.socketFactory.class', 'javax.net.ssl.SSLSocketFactory')
properties.setProperty('mail.imap.socketFactory.fallback', 'false')
properties.setProperty('mail.imap.socketFactory.port', 'your_mail_server_port') // i.e. 993

def session = javax.mail.Session.getDefaultInstance(properties)
def store = session.getStore('imap')
store.connect('your username (usually email address)', 'your_password')

def inbox = store.getFolder('INBOX')
inbox.open(javax.mail.Folder.READ_ONLY)

def onlyFromGivenUser = inbox.search(new javax.mail.search.FromStringTerm('your_sender_address')) // i.e. test+1@gmail.com

onlyFromGivenUser.each { message ->
    if (message.getContent() instanceof Multipart) {
        StringBuilder content = new StringBuilder()
        def multipart = (Multipart) message.getContent()
        multipart.eachWithIndex { Multipart entry, int i ->
            def part = entry.getBodyPart(i)
            if (part.isMimeType('text/plain')) {
                content.append(part.getContent().toString())
            }
        }
        SampleResult.setResponseData(content.toString(), 'UTF-8')
    } else {
        SampleResult.setResponseData(message.getContent().toString(), 'UTF-8')
    }
}

More information:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
1

Not sure if you ever managed, I stepped away from javax.mail.Multipart and have implemented the below code in a JSR223 sampler inside a While Controller, this worked for me.

import javax.mail.Message
import javax.mail.search.RecipientStringTerm

Properties properties = new Properties();
properties.put('mail.imap.host', 'your mail server host') // i.e. imap.gmail.com
properties.put('mail.imap.port', your mail server port)  // i.e. 993
properties.setProperty('mail.imap.socketFactory.class', 'javax.net.ssl.SSLSocketFactory')
properties.setProperty('mail.imap.socketFactory.fallback', 'false')
properties.setProperty('mail.imap.socketFactory.port', 'your_mail_server_port') // i.e. 993

def session = javax.mail.Session.getDefaultInstance(properties)
def store = session.getStore('imap')
store.connect('your username (usually email address)', 'your_password')

def inbox = store.getFolder('INBOX');
inbox.open(javax.mail.Folder.READ_ONLY);

def onlyToGivenUser = inbox.search(new RecipientStringTerm(Message.RecipientType.TO,'your_recipient_address')); // i.e. test+1@gmail.com 

try {
    onlyToGivenUser.each { message ->
        ByteArrayOutputStream emailRaw = new ByteArrayOutputStream();
        message.writeTo(emailRaw);
        SampleResult.setResponseData(emailRaw.toString(), 'UTF-8');
        }
    } catch (Exception ex) {
        log.warn("Something went wrong", ex);
        OUT.println("Something went wrong", ex);
        throw ex;
    }

You probably need additional conditions and not only use the recipients address, in my case the recipient is unique for each iteration

J. Doe
  • 187
  • 2
  • 10