您应该使用实现QuotaAwareStore接口的 store,例如IMAPSSLStore或IMAPStore。在这样的商店和每个配额对象检查“资源”数组上调用“getQuota ”。每个Quota.Resource包含“限制”和“使用”值。
Properties prop = System.getProperties();
String host = "imap.gmail.com";
String username = "user@google.com";
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));
}
}
我的帐户在控制台中的输出:
配额根:''
名称:'STORAGE',限制:'10486380',用法:'1564'
编辑
另一个试图找到配额和文件夹大小之间相关性的例子:
IMAP quota and folder size big example
摘要,基于 JavaMail JavaDoc 和 GMail 上的大示例的结果:
- 几个
Folder
's 可能有相同的Quota
对象。例如,GMailQuota
在所有文件夹之间只有一个共享。
Folder
是逻辑实体(不像文件系统上的文件夹)。例如,GMail 有一个文件夹[Gmail]/All Mail
,其中包含来自其他文件夹的邮件。
limit
GMail 返回的值以千字节为usage
单位。Quota
因此,如果您需要显示您应该使用的剩余空间Quota
。如果您需要显示带有消息的网格并按大小排序,您应该使用folder.getMessages()
and message.getSize()
。