使用 Java 邮件服务器进行测试

发布于 2024-08-31 05:28:29 字数 320 浏览 8 评论 0原文

我正在测试一个应用程序,该应用程序从邮箱中取出邮件,根据该邮件的内容执行某些操作,然后根据操作的结果发送响应邮件。

我正在寻找一种为此应用程序编写测试的方法。理想情况下,我希望这些测试能够启动自己的邮件服务器,将我的测试电子邮件推送到该邮件服务器上的文件夹,并让我的应用程序从我的测试启动的邮件服务器中抓取邮件。

配置应用程序以使用邮件服务器并不困难,但我不知道在哪里可以找到用 Java 启动邮件服务器的编程方法。我看过 JAMES,但我无法弄清楚如何从我的测试中启动服务器。

所以问题是这样的:我可以使用什么来构建可以完全在 Java 中配置和启动的 Java 邮件服务器?

I'm in the process of testing an application that takes mail out of a mailbox, performs some action based on the content of that mail, and then sends a response mail depending on the result of the action.

I'm looking for a way to write tests for this application. Ideally, I'd like for these tests to bring up their own mail server, push my test emails to a folder on this mail server, and have my application scrape the mail out of the mail server that my test started.

Configuring the application to use the mailserver is not difficult, but I do not know where to look for a programatic way of starting a mail server in Java. I've looked at JAMES, but I am unable to figure out how to start the server from within my test.

So the question is this: What can I use for a mail server in Java that I can configure and start entirely within Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我们的影子 2024-09-07 05:28:29

使用起来也非常简单GreenMail,它被设计为用于自动“单元”测试的邮件服务器。

从项目网页(现在可能还有一些其他具有发送/接收功能的工具):

GreenMail 是一个开源的、直观的
和易于使用的电子邮件测试套件
用于测试目的的服务器。支持
SMTP、POP3、IMAP,带 SSL 套接字
支持。 GreenMail 还提供了
JBoss GreenMail 服务。绿色邮件是
第一个也是唯一一个提供
接收双方的测试框架
并从 Java 检索电子邮件。

There is also very simple in use GreenMail which was designer as a mail server for automatic "unit" tests.

From projects web page (probably there are some others tools with sending/receiving functionality nowadays):

GreenMail is an open source, intuitive
and easy-to-use test suite of email
servers for testing purposes. Supports
SMTP, POP3, IMAP with SSL socket
support. GreenMail also provides a
JBoss GreenMail Service. GreenMail is
the fist and only library that offers
a test framework for both receiving
and retrieving emails from Java.

眼藏柔 2024-09-07 05:28:29

我使用过 DumbsterSubEthaSmtp 在单元测试之前测试发送电子邮件的代码。

我发现与 Dumbster 合作要容易得多。

I've used both Dumbster and SubEthaSmtp in unit tests before to test code that sends email.

I found Dumbster to be far easier to work with.

月竹挽风 2024-09-07 05:28:29

看看JES,似乎做你想做的。

Take a look at JES, seems to do what you want.

漆黑的白昼 2024-09-07 05:28:29

Dumster:设置速度很快!但无法处理邮件附件。正文末尾只有字符串,必须单独解析。

所以现在我正在尝试另一个框架

Dumbster: Fast to setup! But can't handle mail attachments. There only strings at the end of the body and have to be parsed separately.

So now I'm trying another framework

半透明的墙 2024-09-07 05:28:29

Mock-JavaMail 项目

是我在为 Jenkins 开发插件时遇到的,它是一个梦想使用!

只需 删除依赖项进入您的项目,您就可以开始了(我将让 Kohsuke 解释如何设置和使用它)。

如果您不耐烦,这里有一个如何使用它的快速示例:

示例:

// Setup test: add mail to inbox
Mailbox tmp = Mailbox.get("[email protected]");
tmp.add(/* your javax.mail.Message */)
assertEquals 1, tmp.size()

// Connect to the inmemory mailbox using "imap"
Session session = Session.getInstance(System.getProperties(), null);
Store store = session.getStore('imap');
store.connect("bar.com","foo","anything");

// Check the mail exists!
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
assertEquals 1, inbox.getMessageCount()
store.close();

The Mock-JavaMail project

I came across it when developing a plugin for Jenkins, and it's been a dream to use!

Just drop the dependency into your project, and you're ready to go (I'll let Kohsuke explain how to set it up and use it).

If you're impatient, here's a quick example of how it's used:

Example:

// Setup test: add mail to inbox
Mailbox tmp = Mailbox.get("[email protected]");
tmp.add(/* your javax.mail.Message */)
assertEquals 1, tmp.size()

// Connect to the inmemory mailbox using "imap"
Session session = Session.getInstance(System.getProperties(), null);
Store store = session.getStore('imap');
store.connect("bar.com","foo","anything");

// Check the mail exists!
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
assertEquals 1, inbox.getMessageCount()
store.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文