简体   繁体   English

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

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

I have to implement an IMAP Client in Java. 我必须用Java实现IMAP客户端。

Which advantages has using the Apache Commons Net library? 使用Apache Commons Net库有哪些优势? Does it make the implementation robust and more flexible? 它是否使实现更健壮,更灵活?

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? 但是,如何将文件夹列表作为文件夹实例而不是纯字符串输出?

Short story : it depends on your real requirements. 简短说明:这取决于您的实际要求。

If your client is mainly focused on sending and reading mail , the JavaMail API is 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 Net library 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 page of the project is built on top of the Java Mail API, which it aims to simplify . 只是为了完成这个答案,你不应该忘记Apache Commons Email ,它根据项目的主页 建立在Java Mail API之上,旨在简化它 It is much closer to JavaMail than to Commons Net. 它更接近JavaMail而不是Commons Net。

Without knowing more of what one wants to do, it is hard to give a more precise answer... 如果不知道自己想要做什么,就很难给出更准确的答案......

Consider looking at Retrieve UnRead Emails from Gmail - JavaMail API + IMAP 考虑查看从Gmail检索UnRead电子邮件 - 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 commons库简单得多。

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 commons库,请查看javadoc并查看可以传递给.select()其他参数。

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. 看起来像apache IMAPClient是IMAP协议的低级包装器,所以没有提供比字符串更好的东西。 For an higher level API, you could look into the JavaMail library: 对于更高级别的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();

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. Yahoo IMAP客户端 :使用Java Futures支持异步IO,但是对于检查新邮件这样的简单用例来说,它是低级别的,可能太复杂了。
  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) Email4J :易于使用的高级API,在下面使用Java Mail,但在Maven中心尚未使用,而大型PR仍在打开(我正在使用fork)

For local testing, I am using the lightweight docker-imap-devel docker image. 对于本地测试,我使用轻量级docker-imap-devel docker镜像。

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:手动IMAP会话中可以找到一个很好的介绍,看看IMAP协议的幕后(有助于调试)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM