Jmeter Groovy JavaMail API multipart 添加内容到示例结果

Jmeter Groovy JavaMail API multipart add content to sample result

查看 中发布的答案,我实际上设法使用了收件人搜索词。

在 JSR223 采样器中使用以下代码

    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. 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')
        }
    }

这非常有效,但当电子邮件为 ContentType 时失败:multipart/MIXED,因为它不会深入到 multipart/RELATED、multipart/ALTERNATIVE 然后到 TEXT/PLAIN 或 TEXT/HTML,我喜欢在上面做一个正则表达式以从正文中提取 link。

猜测我需要一些计数器和一个“if else”,或类似提到的东西 here,但不确定如何转换以适合上面的脚本...

如有任何帮助,我们将不胜感激。

我离开了 javax.mail.Multipart 和 javax.mail.internet.MimeMultipart,并在 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. 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);
        throw ex;
    }

希望有一天这对某人有所帮助。