位置:首页 > Java技术 > JavaMail教程 > JavaMail 限额管理

JavaMail 限额管理

JavaMail配额是限定或固定的号码或邮件的数量在电子邮件存储。JavaMail API为每个邮件服务请求调用计数配额。电子邮件服务可以适用下列配额标准:

  • 外发邮件(包括附件)的最大大小。

  • 邮件信息,包括附件的最大大小。

  • 消息的最大大小时,管理员是一个收件人

对于配额管理的JavaMail有以下类别:

Class 描述
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类

创建一个Java类文件QuotaExample,是其内容如下:

package com.codeinn;

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。一旦你获得了存储对象,获取配额阵列和遍历并打印相关信息。

编译并运行

现在,我们的类是准备好了,让我们编译上面的类。我已经保存了类QuotaExample.java到目录 : /home/manisha/JavaMailAPIExercise. 我们需要javax.mail.jar 和 activation.jar在classpath中。执行下面的命令从命令提示符编译类(两个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'