使用groovy在Jmeter中基于收件人电子邮件ID读取电子邮件 [英] Reading Emails based on recipient email id in Jmeter using groovy
问题描述
我有一个Jmeter脚本,该脚本遵循以下步骤
1.注册用户
2.使用具有以下脚本的Mail Reader Sampler读取电子邮件
I have a Jmeter script which follows the following steps 1. registers user 2. Reads email using Mail Reader Sampler which has following script
StringBuilder aggregateResult = new StringBuilder()
prev.getSubResults().each {
aggregateResult.append(it.getResponseDataAsString())
it.getSubResults().each {
aggregateResult.append(it.getResponseDataAsString())
it.getSubResults().each {
aggregateResult.append(it.getResponseDataAsString())
}
}
}
prev.setResponseData(aggregateResult.toString().getBytes())
- 然后基于正则表达式提取特定链接。
到目前为止,它会读取服务器中的最新电子邮件或所有未读电子邮件。
有人可以帮助我修改上述脚本,以根据在步骤1中创建的用户电子邮件阅读消息吗?电子邮件的创建方式如test + 1 @ gmail.com,test + 2 @ gmail.com等。
As of now, it reads either the latest email or all the unread emails in the server. Can someone please help me to modify the above script to read the message based on the user email created at step 1? Emails are created like test+1@gmail.com, test+2@gmail.com and so on.
推荐答案
不幸的是,它如果您需要获取邮件阅读器采样器,则无法执行此操作仅可用于此或该发件人电子邮件地址的邮件,您可以使用 JavaMail API ,它使用 FromStringTerm 来自 JSR223采样器
Unfortunately it is not something you can do with Mail Reader Sampler, if you need to get mail(s) only for this or that sender email address you can use JavaMail API which provides filtering using i.e. FromStringTerm class from JSR223 Sampler
示例代码:
import javax.mail.Multipart
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 onlyFromGivenUser = inbox.search(new javax.mail.search.FromStringTerm('your_sender_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')
}
}
更多信息:
- Apache Groovy - Why and How You Should Use It
- Filter emails by domain using javamail
这篇关于使用groovy在Jmeter中基于收件人电子邮件ID读取电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!