Search code examples
emailgroovyjmeterjakarta-mailjsr223

Jmeter Groovy JavaMail API multipart add content to sample result


Looking at answers posted in Reading Emails based on recipient email id in Jmeter using groovy I actually managed to use the recipient search term.

Using the below in a JSR223 Sampler

    import javax.mail.Multipart
    import javax.mail.internet.MimeMultipart
    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. [email protected]
    
    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')
        }
    }

This works perfectly, but fails when email is ContentType: multipart/MIXED as it does not drill down to multipart/RELATED, multipart/ALTERNATIVE and then to TEXT/PLAIN or TEXT/HTML, on which I like to do a regex on to extract a link from the body.

Guessing some counter on i is needed and an "if else", or something like mentioned here, but unsure how to convert to fit in the above script...

Any help would be much appreciated.


Solution

  • I stepped away from javax.mail.Multipart and javax.mail.internet.MimeMultipart and have implemented the below code in a While Controller

    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. [email protected] 
    
    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);
            throw ex;
        }
    

    Hope this helps someone one day.