Java 中的 IMAP 客户端:JavaMail API 还是 Apache Commons Net?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16208665/
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-31 22:13:35  来源:igfitidea点击:

IMAP client in Java: JavaMail API or Apache Commons Net?

javaemailimapjavamailapache-commons

提问by superuser000001

I have to implement an IMAP Client in Java.

我必须用 Java 实现一个 IMAP 客户端。

Which advantages has using the Apache Commons Net library? Does it make the implementation robust and more flexible?

使用 Apache Commons Net 库有哪些优势?它是否使实现更健壮和更灵活?

How do I have to handle return values, it always produces strings.

我如何处理返回值,它总是产生字符串。

For example:

例如:

public static void main(String[] args) throws Exception {
    IMAPClient client = new IMAPClient();
    client.connect(SERVER);
    client.login(USERNAME, PASSWORD);
    client.select("INBOX");
    client.fetch("1", "body[header]");
}

and we can direct the output to string by

我们可以将输出定向到字符串

client.addProtocolCommandListener(new PrintCommandListener(System.out, true));

But how can I get a list of folders as folder instances instead of pure string output?

但是如何获取文件夹列表作为文件夹实例而不是纯字符串输出?

采纳答案by Serge Ballesta

Short story : it depends on your real requirements.

简短的故事:这取决于您的实际需求。

If your client is mainly focused on sending and reading mail, the JavaMail APIis a de-facto standard high-level API, and it will be much simpler to compose mail, add headers and/or attachements.

如果您的客户端主要专注于发送和阅读邮件,那么JavaMail API是事实上的标准高级 API,编写邮件、添加标题和/或附件会简单得多。

On the other hand, if you intend to offer all the possibilities of the IMAP protocol, the lower-level Apache Commons Netlibrary will allow more detailed operations, at the cost of more boiler plate code for simple operations.

另一方面,如果您打算提供 IMAP 协议的所有可能性,较低级别的Apache Commons Net库将允许更详细的操作,但代价是为简单操作增加更多样板代码。

Just to complete this answer, you should not forget Apache Commons Email, which according to the home pageof the project is built on top of the Java Mail API, which it aims to simplify. It is much closer to JavaMail than to Commons Net.

只是为了完成这个答案,你不应该忘记Apache Commons Email,根据项目的主页,它是建立在 Java Mail API 之上的,它旨在简化. 它更接近 JavaMail 而不是 Commons Net。

Without knowing more of what one wants to do, it is hard to give a more precise answer...

在不知道更多人想要做什么的情况下,很难给出更准确的答案......

回答by Gregor

There are modern alternatives I recently checked out:

我最近检查了一些现代替代品:

  1. Yahoo IMAP client: supports async IO using Java Futures, but is low level and maybe too complicated for simple use cases like checking for new mail.
  2. Email4J: easy-to-use, high-level API that uses Java Mail beneath, but not in Maven central yet and a large PR is still open (I am using the fork)
  1. Yahoo IMAP 客户端:支持使用 Java Futures 的异步 IO,但级别较低,对于简单的用例(例如检查新邮件)来说可能过于复杂。
  2. Email4J:易于使用的高级 API,在下面使用 Java Mail,但尚未在 Maven 中心,并且一个大型 PR 仍然开放(我正在使用 fork)

For local testing, I am using the lightweight docker-imap-develdocker image.

对于本地测试,我使用的是轻量级的docker-imap-develdocker 镜像。

A good introduction, looking behind the scenes of the IMAP protocol (helpful for debugging), can be found at IMAP 101: Manual IMAP Sessions.

可以在IMAP 101: Manual IMAP Sessions 中找到一个很好的介绍,了解 IMAP 协议的幕后(有助于调试)。

Update: In the end, it was the easiest to go with good old javax.mail.imap APIand Folder.open()which worked out of the box, should have checked that earlier… There's also an experimental IdleManagerthat can be used to watch a mailbox for new messages with a supplied ExecutorService.

更新:最后,使用旧的javax.mail.imap API是最容易的,并且Folder.open()开箱即用,应该早点检查...还有一个实验性的IdleManager,可用于观察邮箱中的新消息带有提供的ExecutorService.

回答by Fallso

Consider looking at Retrieve UnRead Emails from Gmail - JavaMail API + IMAP

考虑查看从 Gmail 检索未读电子邮件 - JavaMail API + IMAP

It's coded using the JavaMail API, but in my opinion this has a much simpler interface than the Apache commons library.

它是使用 JavaMail API 编码的,但在我看来,它的接口比 Apache 公共库简单得多。

If you really want to use the Apache commons library, have a look at the javadocs and see what other parameters you can pass to .select().

如果您真的想使用 Apache 公共库,请查看 javadocs 并查看您可以将哪些其他参数传递给.select().

回答by Fabien Benoit-Koch

how can i get list of folder as folder instances instead of pure string output?

如何将文件夹列表作为文件夹实例而不是纯字符串输出?

It looks like apache IMAPClient is a low-level wrapper around the IMAP protocol, so nothing fancier than strings are provided. For an higher level API, you could look into the JavaMaillibrary:

看起来 apache IMAPClient 是 IMAP 协议的低级包装器,因此没有提供比字符串更高级的东西。对于更高级别的 API,您可以查看JavaMail库:

Session session = Session.getDefaultInstance(System.getProperties(),null);
Store store = session.getStore("imaps");
store.connect(this.host, this.userName, this.password);

// Get default folder
Folder folder = store.getDefaultFolder();

// Get any folder by name
Folder[] folderList = folder.list();