Sample code for 30+ languages & platforms
Java

IMAP Get Quota Root

Sends the GETQUOTAROOT command to the IMAP server to get the quota root for a given mailbox. This also provides quota information for the given mailbox.

The GetQuotaRoot method is available in Chilkat starting at v9.5.0.58, and is only available for IMAP servers that support the QUOTA extension.

The information is returned in JSON format. This example demonstrates how to parse the JSON to get the desired information.

Typically, the quota root for the Inbox is the empty string.

Chilkat Java Downloads

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

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

        // Use TLS
        imap.setSsl(true);
        imap.setPort(993);
        String caps;
        try {
            imap.connect("MY-IMAP-DOMAIN");

            // Authenticate
            imap.login("MY-IMAP-LOGIN", "MY-IMAP-PASSWORD");

            // First check to see if the IMAP server supports the QUOTA extension.
            caps = imap.capability();
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        if (!imap.hasCapability("QUOTA", caps)) {
            System.out.println("IMAP server does not support the QUOTA extension.");
            return;
        }

        String jsonStr;
        try {
            jsonStr = imap.getQuotaRoot("Inbox");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // The JSON string will look something like this:
        // {"QUOTAROOT":{"mailbox":"Inbox","root":""},"QUOTA":{"root":"","resource":"STORAGE","used":22216,"max":15728640}}
        System.out.println(jsonStr);

        // This is easy to parse...
        JsonObject json = new JsonObject();
        json.load(jsonStr);

        // Get the quota root for Inbox, which is an empty string in the case above.
        String quotaRoot = json.stringOf("QUOTAROOT.root");
        System.out.println("Quota Root for Inbox: [" + quotaRoot + "]");

        // Now get the resource name (which can be STORAGE or MESSAGE) and the used/max values.
        // Most servers have STORAGE quotas, which are for total capacity.  For example, GMail's
        // STORAGE quota is 15GB (at the time of writing this example).
        // A MESSAGE quota sets a limit on the number of messages.
        String resourceName = json.stringOf("QUOTA.resource");
        int quotaUsed = json.intOf("QUOTA.used");
        int quotaMax = json.intOf("QUOTA.max");

        System.out.println("Resource: " + resourceName);
        System.out.println("Quota Used (in KB): " + quotaUsed);
        System.out.println("Quota Max (in KB): " + quotaMax);

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