JavaMail API – Quota Management

JavaMail API – Quota Management

A quota in JavaMail is a limited or fixed number or amount of messages in a email store. Each Mail service request counts toward the JavaMail API Calls quota. An email service can apply following quota criterion:

  • Maximum size of outgoing mail messages, including attachments.
  • Maximum size of incoming mail messages, including attachments.
  • Maximum size of message when an administrator is a recipient

For Quota management JavaMail has following classes:

Class Description
public class Quota This class represents a set of quotas for a given quota root. Each quota root has a set of resources, represented by the Quota.Resource class. Each resource has a name (for example, “STORAGE”), a current usage, and a usage limit. This has only one methodsetResourceLimit(String name, long limit).
public static class Quota.Resource Represents an individual resource in a quota root.
public interface QuotaAwareStore An interface implemented by Stores that support quotas. The getQuota and setQuotamethods support the quota model defined by the IMAP QUOTA extension. GmailSSLStore, GmailStore, IMAPSSLStore, IMAPStore are the known implementing classes of this interface.

Let us see and example in the following sections which checks for mail storage name, limit and its usage.

Create Java Class

Create a java class file QuotaExample, the contents of which are as follows:

package com.webmastersbuzz;

import java.util.Properties;

import javax.mail.Quota;
import javax.mail.Session;
import javax.mail.Store;

import com.sun.mail.imap.IMAPStore;

public class QuotaExample 
   public static void main(String[] args) 
         Properties properties = new Properties();
         properties.put("", "imaps");
         properties.put("mail.imaps.port", "993");
         properties.put("mail.imaps.starttls.enable", "true");
         Session emailSession = Session.getDefaultInstance(properties);
         // emailSession.setDebug(true);

         // create the IMAP3 store object and connect with the pop server
         Store store = emailSession.getStore("imaps");

         //change the user and password accordingly
         store.connect("", "", "*****");
         IMAPStore imapStore = (IMAPStore) store;
         System.out.println("imapStore ---" + imapStore);

         //get quota
         Quota[] quotas = imapStore.getQuota("INBOX");
         //Iterate through the Quotas
         for (Quota quota : quotas) {
            //Iterate through the Quota Resource
            for (Quota.Resource resource : quota.resources) {
                  "name:'%s', limit:'%s', usage:'%s'",,
                  resource.limit, resource.usage));
      } catch (Exception e) 

Here are connection to the gmail service via IMAP ( server, as IMAPStore implements the QuotaAwareStore. Once you get the Store object, fetch the Quota array and iterate through it and print the relevant information.

Compile and Run

Now that our class is ready, let us compile the above class. I’ve saved the class to directory : /home/manisha/JavaMailAPIExercise. We would need the jars javax.mail.jar and activation.jar in the classpath. Execute the command below to compile the class (both the jars are placed in /home/manisha/ directory) from command prompt:

javac -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar:

Now that the class is compiled, execute the below command to run:

java -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: QuotaExample

Verify Output

You should see a similar message on the command console:

imapStore ---imaps://
name:'STORAGE', limit:'15728640', usage:'513'

Was this article helpful?

Related Articles

Leave A Comment?