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

apache-commonsemailimapjakarta-mailjava

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?

Best Answer

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

Related Topic