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

4

30

I have to implement an IMAP Client in Java.

Which advantages has using the Apache Commons Net library? 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?

Ayakoayala answered 25/4, 2013 at 7:19 Comment(3)
Can't believe there hasn't been any decent answers to this question.Narrow
There is a great Apache Commons Mail API, why not use that?Damage
This question should be closed as off-topic, being a request for recommendation.Uncanonical
B
23

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.

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.

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. It is much closer to JavaMail than to Commons Net.

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

Butterscotch answered 20/10, 2014 at 21:15 Comment(4)
Apache Commons Email, built on top of the JavaMail API, which it aims to simplify And does it actually simplify anything?Exorbitant
@Exorbitant : to be honest, I never used it and only cited what was written on project home page.Butterscotch
Personnal opinion : Commons mail is "simpler" in that its API tries to think of email as a user does, while the JavaMail API exposes the MIME-side of the story. Technically, Javamail is a natural API. So "adding an attachment" in Javamail means converting from SinglePart to Multipart Mime, dealing with each part's headers, Q-Encoding this or that, and all. Commons Mail will have an addAttachment method that will do all that, deal with filenames, encodings... But when it comes down to advanced configuration like, say, enabling IMAP/SSL and SMTP authentication, both are equally footed.Ernaline
I have implemented both of them in a single system. In terms of implementation, both of them have pros and cons. github.com/r0hi7/TrashemailMesne
B
3

Consider looking at Retrieve UnRead Emails from 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.

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

Baroque answered 8/7, 2013 at 16:0 Comment(0)
N
3

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 JavaMail library:

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();
Noellenoellyn answered 20/10, 2014 at 19:4 Comment(0)
L
3

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)

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

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

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

Lafontaine answered 5/9, 2019 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.