📅  最后修改于: 2020-11-14 10:05:11             🧑  作者: Mango
JavaMail中的配额是电子邮件存储中的消息数量有限或固定。每个邮件服务请求都计入JavaMail API调用配额。电子邮件服务可以应用以下配额标准:
包含附件的外发邮件的最大大小。
包括附件在内的传入邮件的最大大小。
管理员为收件人时邮件的最大大小
对于配额管理,JavaMail具有以下类:
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 method setResourceLimit(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 setQuota methods support the quota model defined by the IMAP QUOTA extension. GmailSSLStore, GmailStore, IMAPSSLStore, IMAPStore are the known implementing classes of this interface. |
在以下各节中,让我们看一下示例,检查邮件存储名称,限制及其用法。
创建一个Java类文件QuotaExample ,其内容如下:
package com.tutorialspoint;
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)
{
try
{
Properties properties = new Properties();
properties.put("mail.store.protocol", "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("imap.gmail.com", "abc@gmail.com", "*****");
IMAPStore imapStore = (IMAPStore) store;
System.out.println("imapStore ---" + imapStore);
//get quota
Quota[] quotas = imapStore.getQuota("INBOX");
//Iterate through the Quotas
for (Quota quota : quotas) {
System.out.println(String.format("quotaRoot:'%s'",
quota.quotaRoot));
//Iterate through the Quota Resource
for (Quota.Resource resource : quota.resources) {
System.out.println(String.format(
"name:'%s', limit:'%s', usage:'%s'", resource.name,
resource.limit, resource.usage));
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
这是IMAPStore实现QuotaAwareStore时通过IMAP(imap.gmail.com)服务器与gmail服务的连接。获得Store对象后,获取Quota数组并对其进行遍历并打印相关信息。
现在我们的课程已经准备好,让我们编译上面的课程。我已经将类QuotaExample.java保存到目录: / home / manisha / JavaMailAPIExercise 。我们将在类路径中需要jars javax.mail.jar和activation.jar 。从命令提示符处执行以下命令来编译类(两个罐子都放在/ home / manisha /目录中):
javac -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: QuotaExample.java
现在已经编译了该类,请执行以下命令来运行:
java -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: QuotaExample
您应该在命令控制台上看到类似的消息:
imapStore ---imaps://abc%40gmail.com@imap.gmail.com
quotaRoot:''
name:'STORAGE', limit:'15728640', usage:'513'