Om M
Om M

Reputation: 201

Finding available mail storage, free space etc using javax.mail (or similar)

What can be done to know users' mail storage size, how much free space is available and how much total space is available, using javax.mail or any related library of Java?

I get the information about messages by using javax.mail api.

Message message = new MimeMessage(session);
folder1 = store.getDefaultFolder();
folder1 = folder1.getFolder(boxType);
System.out.println("folder found :" 
                  + folder1.exists() 
                  + "folder name is " 
                  + folder1.getFullName());
folder1.open(Folder.READ_ONLY);
messages = folder1.getMessages();

Upvotes: 4

Views: 3143

Answers (1)

Vadim Ponomarev
Vadim Ponomarev

Reputation: 1346

You should use store which implements QuotaAwareStore interface, such as IMAPSSLStore or IMAPStore. Call "getQuota" on such store and on each Quota object check array of "resources". Each Quota.Resource contains "limit" and "usage" values.

    Properties prop = System.getProperties();

    String host = "imap.gmail.com";
    String username = "[email protected]";
    String password = "some-password";

    prop.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    prop.setProperty("mail.imap.host", host);
    prop.setProperty("mail.imap.port", "993");
    prop.setProperty("mail.imap.starttls.enable", "true");
    prop.setProperty("mail.imap.socketFactory.fallback", "false");
    prop.setProperty("mail.debug", "true");

    Session ses = Session.getInstance(prop, null);
    Store store = ses.getStore("imap");
    store.connect(host, username, password);

    if (!IMAPStore.class.isInstance(store))
        throw new IllegalStateException("Is not IMAPStore");

    IMAPStore imapStore = (IMAPStore) store;
    Quota[] quotas = imapStore.getQuota("INBOX");

    for (Quota quota : quotas) {
        System.out.println(String.format("quotaRoot:'%s'", quota.quotaRoot));

        for (Quota.Resource resource : quota.resources) {
            System.out.println(String.format("name:'%s', limit:'%s', usage:'%s'",
                    resource.name, resource.limit, resource.usage));
        }
    }

Output in console for my account:

quotaRoot:''
name:'STORAGE', limit:'10486380', usage:'1564'

EDIT
And another example in attempt to find correlation between quota and folder size:
IMAP quota and folder size big example

Summary, based on JavaMail JavaDoc and results from big example on GMail:

  • Several Folder's could have same Quota object. GMail, for example, have only one Quota shared between all folders.
  • Folder is logical entity (not like folder on file system). For example, GMail have folder [Gmail]/All Mail which contains messages from other folders.
  • limit and usage values returned for Quota by GMail measured in kilobytes.

So if you need to show how much space left you should use Quota. And if you need to display grid with messages and sorting by size you should use folder.getMessages() and message.getSize().

Upvotes: 5

Related Questions