Jmeter Groovy JavaMail API 多部分向示例结果添加内容
Posted
技术标签:
【中文标题】Jmeter Groovy JavaMail API 多部分向示例结果添加内容【英文标题】:Jmeter Groovy JavaMail API multipart add content to sample result 【发布时间】: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,但不确定如何转换以适应上述脚本...
任何帮助将不胜感激。
【问题讨论】:
【参考方案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;
希望有一天这对某人有所帮助。
【讨论】:
以上是关于Jmeter Groovy JavaMail API 多部分向示例结果添加内容的主要内容,如果未能解决你的问题,请参考以下文章
JMeter中Groovy和BeanShell脚本的性能比较