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?
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', 'your-username@gmail.com', 'your-password')
inbox = store.getFolder('INBOX')
inbox.open(javax.mail.Folder.READ_ONLY)
def message = inbox.getMessage(inbox.total)
return message.getContent()
More information:
Hi Dimitri T, thank you for your reply. I hope your answer will work. But I have tried another approach as I needed a solution for urgent. I use a Gmail filter and filter the message from the sender to another folder. Then I read the email from that folder using Mail Reader Sampler then I delete that email from that folder. In this way, I will always have only one email in that folder at a time Thank you anyway for your approach. I will try next time, I am sure your answer will works.