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

Subscribe to Mailboxes and List Subscribed Mailboxes

See more IMAP Examples

Demonstrates how to list subscribed mailboxes, and unsubscribe/subscribe to a mailbox.

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("myLogin", "myPassword");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // First examine the already subscribed mailboxes.
        String refName = "";
        String wildcardedMailbox = "*";
        boolean subscribed = true;

        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;
        }

        System.out.println("----");

        // Sample output.

        // INBOX
        // INBOX/recent
        // INBOX/misc
        // INBOX/misc/solutions
        // ...
        // Outbox
        // Deleted Items
        // Sent Items
        // Drafts
        // Junk E-mail
        // Trash
        // Sent
        // Templates

        // Let's unsubscribe to "INBOX/misc/solutions".
        try {
            imap.unsubscribe("INBOX/misc/solutions");

            // Get the list of subscribed mailboxes again to verify that "INBOX/misc/solutions" is missing.
            imap.mbxList(subscribed, "", "INBOX/misc*", mboxes);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

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

        System.out.println("----");

        // Re-subscribe to "INBOX/misc/solutions".
        try {
            imap.subscribe("INBOX/misc/solutions");

            // Get the list of subscribed mailboxes again to verify that "INBOX/misc/solutions" is back in the list.
            imap.mbxList(subscribed, "", "INBOX/misc*", mboxes);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

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

        System.out.println("----");

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