Trillian
Trillian

Reputation: 411

How to get messages from subfolders using JavaMail?

I am using JavaMail API to connect to outlook and read messages. I have list of sub folders under INBOX in my outlook account. I am able to get all messages from Inbox using:

Store store = session.getStore("imaps");
store.connect("outlook.office365.com", 993, MY_MAIL, MY_PASS);
Folder inbox = store.getFolder("Inbox");

But i am not able to read messages from sub folders. To read messages from a sub folder called 'subFolder' i have tried:

Folder subFolder = store.getFolder("subFolder");
Folder subFolder = store.getFolder("Inbox\subFolder");
Folder subFolder = store.getFolder("Inbox.subFolder");

I am getting a FolderNotFoundException

javax.mail.FolderNotFoundException: Inbox.subFolder not found
    at com.sun.mail.imap.IMAPFolder.checkExists(IMAPFolder.java:452)
    at com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:1040)
    at com.sun.mail.imap.IMAPFolder.open(IMAPFolder.java:973)

Upvotes: 4

Views: 3619

Answers (2)

JimiLoe
JimiLoe

Reputation: 978

You probably use a wrong IMAP folder separator. The IMAP folder separator is not standardized and can be determined like this:

char separator = store.getDefaultFolder().getSeparator();

The most common separators are "." and "/". I guess that that "/" was the right choice in your case. store.getFolder() is able to access nested subfolders:

// assuming that "/" is the right separator
Folder folder = store.getFolder("inbox/subfolder"); 

Upvotes: 1

Hari Prasad
Hari Prasad

Reputation: 304

The IMAPFolder API Documentation says there is a method getFolder() on the Folder. Once you get the Inbox folder, call getFolder() on this folder passing the subfolder name.

Here is the reference https://eclipse-ee4j.github.io/javamail/docs/api/com/sun/mail/imap/IMAPFolder.html

Upvotes: 3

Related Questions