Javamail Api 简明教程
JavaMail API - Quota Management
JavaMail 中的配额是指电子邮件存储中一条消息的限制或固定数量或金额。每次邮件服务请求均计入 JavaMail API 调用配额。电子邮件服务可以应用以下配额标准:
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
对于配额管理,JavaMail 有以下类:
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 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. |
让我们在以下部分中查看一个示例,该示例检查邮件存储名称、限制及其使用情况。
Let us see and example in the following sections which checks for mail storage name, limit and its usage.
Create Java Class
创建一个 java 类文件 QuotaExample ,其内容如下:
Create a java class file QuotaExample, the contents of which are as follows:
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();
}
}
}
以下是通过 IMAP(imap.gmail.com)服务器连接到 gmail 服务,因为 IMAPStore 实施了 QuotaAwareStore。一旦你获取 Store 对象,获取配额数组并遍历它,打印相关信息。
Here are connection to the gmail service via IMAP (imap.gmail.com) 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
现在我们的类已经准备好了,让我们编译上述类。我已将类 QuotaExample.java 保存到目录 /home/manisha/JavaMailAPIExercise 中。我们需要 classpath 中的 jars javax.mail.jar 和 activation.jar。从命令提示符执行以下命令来编译类(这两个 jars 都放置在 /home/manisha/ 目录中):
Now that our class is ready, let us compile the above class. I’ve saved the class QuotaExample.java 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: QuotaExample.java
现在类已编译,执行以下命令来运行:
Now that the class is compiled, execute the below command to run:
java -cp /home/manisha/activation.jar:/home/manisha/javax.mail.jar: QuotaExample