【问题标题】:Jmeter Groovy JavaMail API multipart add content to sample resultJmeter Groovy JavaMail API 多部分向示例结果添加内容
【发布时间】:2020-11-03 21:49:13
【问题描述】:

查看Reading Emails based on recipient email id in Jmeter using groovy 中发布的答案,我实际上设法使用了收件人搜索词。

在 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,我喜欢在上面做一个正则表达式从正文中提取链接。

需要在 i 上猜一些计数器和一个“if else”,或类似提到的 here,但不确定如何转换以适应上述脚本...

任何帮助将不胜感激。

【问题讨论】:

    标签: email groovy jmeter jakarta-mail jsr223


    【解决方案1】:

    我放弃了 javax.mail.Multipart 和 javax.mail.internet.MimeMultipart 并在 While 控制器中实现了以下代码

    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;
        }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 2022-01-18
      • 2023-01-29
      • 2017-04-26
      • 2015-07-09
      • 1970-01-01
      • 2019-10-24
      相关资源
      最近更新 更多