【问题标题】:Is there a way to fetch latest email from "Mail reader sampler" or "Beanshell Sampler"有没有办法从“Mail reader sampler”或“Beanshell Sampler”获取最新的电子邮件
【发布时间】:2017-11-20 14:09:04
【问题描述】:

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

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

【问题讨论】:

标签: email jmeter


【解决方案1】:

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

根据 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 指南。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 2020-12-13
    相关资源
    最近更新 更多