有没有办法从“Mail reader sampler”或“Beanshell Sampler”

时间:2017-06-18 06:14:26

标签: email jmeter

我可以通过“Mail Reader Sampler”监听器使用POP3从我的电子邮件帐户中获取电子邮件。但它没有检索最新的电子邮件。 是否可以使用Beanshell Sampler提取最新的电子邮件。如果是,如果可以实现,请分享代码。

如下面的讨论 - 看起来不可行。但是,想用任何方法检查这是否可以实现? Stackoverflow Discussion on how to fetch required email

1 个答案:

答案 0 :(得分:0)

您可以通过编程方式执行此操作,请查看以下方法:

根据JavaDoc

  

邮件的编号从1开始到文件夹中的邮件总数。

因此,最后一条消息的编号将始终与给定文件夹中的消息总数相同。

使用POP3协议读取上一封电子邮件的示例代码

import javax.mail.Folder
import javax.mail.Message
import javax.mail.Session
import javax.mail.Store

String host = "host"
String user = "username"
String password = "password"

Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties)
Store store = session.getStore("pop3")
store.connect(host, user, password)
Folder inbox = store.getFolder("Inbox")
inbox.open(Folder.READ_ONLY)

int msgCount = inbox.getMessageCount()
Message last = inbox.getMessage(msgCount)

//do what you need with the "last" message
inbox.close(true)
store.close()

我还建议忘记Beanshell,无论何时需要执行脚本 - 使用JSR223 ElementsGroovy language因为Groovy具有更好的性能,它更符合Java并且具有一些不错的语言功能。有关详细信息,请参阅Apache Groovy - Why and How You Should Use It指南。

相关问题