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

List IMAP Mailboxes with Reference

Demonstrates how to list all sub-mailboxes within a specified context. In this case, we list all mailboxes under "INBOX.vendors".

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("imap.example.com");

            // Login
            imap.login("admin@chilkatsoft.com", "myPassword");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Get the list of mailboxes.
        String refName = "";
        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.vendors.paypal
        // INBOX.vendors.dell
        // INBOX.vendors.inMotion
        // INBOX.vendors.myhosting
        // INBOX.vendors.peer1

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