Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

List IMAP Mailboxes

List the mailboxes available within an IMAP account.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.Mailboxes;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Imap imap = new Imap();

        // Connect to an IMAP server.
        // Use TLS
        imap.setSsl(true);
        imap.setPort(993);
        try {
            imap.connect("MY-IMAP-DOMAIN");

            // Login
            imap.login("MY-IMAP-LOGIN", "MY-IMAP-PASSWORD");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        String refName = "";
        // refName is usually set to an empty string.
        // A non-empty reference name argument is the name of a mailbox or a level of
        // mailbox hierarchy, and indicates the context in which the mailbox
        // name is interpreted.

        // Select all mailboxes matching this pattern:
        String wildcardedMailbox = "*";

        boolean subscribed = false;

        Mailboxes mboxes = new Mailboxes();
        try {
            imap.mbxList(subscribed, refName, wildcardedMailbox, mboxes);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        int i = 0;
        while (i < mboxes.getCount()) {
            System.out.println(mboxes.getName(i));
            i = i + 1;
        }

        // Sample output looks like this:
        // INBOX.vendors.shareit
        // INBOX.oldSupport
        // INBOX.vendors.paypal
        // INBOX.sales
        // INBOX.lists
        // INBOX.Drafts
        // INBOX.vendors.dell
        // INBOX.Trash
        // INBOX.invoiceRequests
        // INBOX.purchases
        // INBOX.vendors.inMotion
        // INBOX.oldEmail
        // INBOX.vendors
        // INBOX.lists.python
        // INBOX.vendors.myhosting
        // INBOX.Templates
        // INBOX.friends
        // INBOX.bounceSamples
        // INBOX.lists.ruby
        // INBOX.vendors.peer1
        // INBOX.Sent
        // INBOX.Junk
        // INBOX

        // Disconnect from the IMAP server.
        imap.disconnect();
    }
}