我有一个遵循以下步骤的Jmeter脚本:1.注册用户2.使用具有以下脚本的Mail Reader Sampler读取电子邮件
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等。
不幸的是,您不能使用Mail Reader Sampler进行任何操作,如果您只需要为此或该发件人的电子邮件地址获取邮件,则可以使用JavaMail API,该API使用JSR223 Sampler中的FromStringTerm类进行过滤
示例代码:
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') } }
更多信息: