Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 832
benchpresser Avatar asked Apr 25 '13 07:04

benchpresser


People also ask

What is the Java Mail API?

The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. The JavaMail API is available as an optional package for use with the Java SE platform and is also included in the Java EE platform.

Which protocol is used in Java mail?

In addition to the non-SSL JavaMail protocols "imap", "pop3", and "smtp", the protocols "imaps", "pop3s", and "smtps" can be used to connect to the corresponding services using an SSL connection.

How do I add email to Java?

Browse to the location of your library and Add it. Expand the javamail library. Click Source attachment and click the Edit button. Browse to the location of your source zip file and add it.


3 Answers

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

like image 65
Serge Ballesta Avatar answered Oct 02 '22 07:10

Serge Ballesta


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

like image 41
Fallso Avatar answered Oct 02 '22 05:10

Fallso


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();
like image 24
Fabien Benoit-Koch Avatar answered Oct 02 '22 07:10

Fabien Benoit-Koch