Search code examples
jmeterweb-api-testing

How to read latest email in Gmail using Jmeter?


my test scenario is if I perform some action using API, then I am receiving an email.

I have used Mail Reader Sampler to read an email. I can successfully read the email. But the problem is it reads the oldest email. I want to read the latest email.

How can I read the latest email?

I've attached the screenshot of my configuration. enter image description here


Solution

  • If you want to get the single latest message and avoid receiving all of them you will have to switch to JSR223 Sampler and do this in Groovy language, example code:

    props.setProperty('mail.transport.protocol', 'imaps')
    props.setProperty('mail.imap.host', 'imap.gmail.com')
    props.setProperty('mail.imap.port', '995')
    props.setProperty('mail.imap.ssl.enable', 'true');
    
    def session = javax.mail.Session.getDefaultInstance(props, null)
    store = session.getStore('imaps')
    store.connect('imap.gmail.com', '[email protected]', 'your-password')
    inbox = store.getFolder('INBOX')
    inbox.open(javax.mail.Folder.READ_ONLY)
    def message = inbox.getMessage(inbox.total)
    return message.getContent()
    

    More information: