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

22,756

Solution 1

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

Solution 2

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

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

Solution 4

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.

Share:
22,756
benchpresser
Author by

benchpresser

react redux javascript css, java mail api, webservices

Updated on September 11, 2020

Comments

  • benchpresser
    benchpresser over 3 years

    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?