Javamail-api-quota-management

提供:Dev Guides
移動先:案内検索

JavaMail API-クォータ管理

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クラスを作成する

Javaクラスファイル QuotaExample を作成します。その内容は次のとおりです。

package com.finddevguides;

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", "[email protected]", "*****");
         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 に保存しました。 クラスパスに_javax.mail.jar_および_activation.jar_の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

出力を検証する

コマンドコンソールに同様のメッセージが表示されます。

imapStore ---imaps://abc%[email protected]
quotaRoot:''
name:'STORAGE', limit:'15728640', usage:'513'