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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2782546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:48:35  来源:igfitidea点击:

Working with a Java Mail Server for Testing

javatestingemailmail-server

提问by Charles Dimino

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.

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

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

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

回答by Marcin Zaj?czkowski

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

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

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.

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

回答by matt b

I've used both Dumbsterand SubEthaSmtpin unit tests before to test code that sends email.

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

I found Dumbster to be far easier to work with.

我发现 Dumbster 更容易使用。

回答by Yishai

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

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

回答by Mirko

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.

Dumbster:快速设置!但是不能处理邮件附件。正文末尾只有字符串,必须单独解析。

So now I'm trying another framework

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

回答by Nick Grealy

The Mock-JavaMailproject

模拟,JavaMail的项目

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

我在为 Jenkins 开发插件时遇到了它,并且一直梦想使用它!

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

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

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();