配额管理(Quota Management)

优质
小牛编辑
121浏览
2023-12-01

JavaMail中的配额是电子邮件存储中的有限或固定数量或消息量。 每个邮件服务请求都计入JavaMail API调用配额。 电子邮件服务可以应用以下配额标准:

  • 传出邮件的最大大小,包括附件。

  • 传入邮件的最大大小,包括附件。

  • 管理员是收件人时的最大邮件大小

对于配额管理,JavaMail具有以下类:

描述
public class Quota此类表示给定配额根的一组配额。 每个配额根都有一组资源,由Quota.Resource类表示。 每个资源都有一个名称(例如,“STORAGE”),当前使用情况和使用限制。 这只有一个方法setResourceLimit(String name, long limit)
public static class Quota.Resource表示配额根目录中的单个资源。
public interface QuotaAwareStore由商店实施的支持配额的界面。 getQuotasetQuota方法支持IMAP QUOTA扩展定义的配额模型。 GmailSSLStore, GmailStore, IMAPSSLStore, IMAPStore是此接口的已知实现类。

让我们在以下部分中查看和示例,它们检查邮件存储名称,限制及其用法。

创建Java类 (Create Java Class)

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

package cn.xnip;
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对象后,获取Quota数组并遍历它并打印相关信息。

编译和运行 (Compile and Run)

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

验证输出 (Verify Output)

您应该在命令控制台上看到类似的消息:

imapStore ---imaps://abc%40gmail.com@imap.gmail.com
quotaRoot:''
name:'STORAGE', limit:'15728640', usage:'513'