Exchange Server のルールの操作

ルールの管理

Aspose.Email for Java は、Exchange Server 上のルール管理に使用できます。 EWSClient クラス。このクラスは Exchange Web Services (EWS) を使用し、Exchange Server 2007 以降で利用可能です。ルールの管理方法を示すために、この記事では次のことを説明します:

  • サーバー上の既存ルールを読み取る。
  • 新しいルールを作成する。
  • 既存のルールを更新する。

この記事で説明するすべての機能には Microsoft Exchange Server 2010 Service Pack 1 が必要です。

ルールの読み取り

Exchange Server からすべてのルールを取得するには:

  1. IEWSClient クラスを使用して Exchange Server に接続します。
  2. IEWSClient.getInboxRules() メソッドを呼び出してすべてのルールを取得します。
  3. foreach ループで全ルールを巡回し、条件、アクション、名前などのルールプロパティを表示します。

以下のコードスニペットは、ルールを読み取る方法を示しています。

IEWSClient client = EWSClient.getEWSClient(mailboxURI, credential);

System.out.println("Connected to Exchange server");

// Get all Inbox Rules
InboxRule[] inboxRules = client.getInboxRules();

// Display information about each rule
for (InboxRule inboxRule : inboxRules) {
    System.out.println("Display Name: " + inboxRule.getDisplayName());

    // Check if there is a "From Address" condition
    if (inboxRule.getConditions().getFromAddresses().size() > 0) {
        for (MailAddress fromAddress : (Iterable<MailAddress>) inboxRule.getConditions().getFromAddresses()) {
            System.out.println("From: " + fromAddress.getDisplayName() + " - " + fromAddress.getAddress());
        }
    }
    // Check if there is a "Subject Contains" condition
    if (inboxRule.getConditions().containsSubjectStrings().size() > 0) {
        // foreach to while statements conversion
        for (String subject : inboxRule.getConditions().containsSubjectStrings()) {
            System.out.println("Subject contains: " + subject);
        }
    }
    // Check if there is a "Move to Folder" action
    if (inboxRule.getActions().getMoveToFolder().length() > 0) {
        System.out.println("Move message to folder: " + inboxRule.getActions().getMoveToFolder());
    }
}

新しいルールの作成

Exchange Server 上で新しいルールを作成するには、以下の手順を実行します:

  1. IEWSClient クラスを使用して Exchange Server に接続します。
  2. InboxRule クラスの新しいインスタンスを作成し、以下の必須プロパティを設定します:
    1. 表示名
    2. 条件
    3. アクション
  3. IEWSClient.createInboxRule() メソッドを呼び出してルールを作成します。

以下のコードスニペットは、新しいルールを作成する方法を示しています。

IEWSClient client = EWSClient.getEWSClient(mailboxURI, credential);

System.out.println("Connected to Exchange server");

InboxRule rule = new InboxRule();
rule.setDisplayName("Message from client ABC");

// Add conditions
RulePredicates newRules = new RulePredicates();
// Set Subject contains string "ABC" and Add the conditions
newRules.containsSubjectStrings().addItem("ABC");
newRules.getFromAddresses().addMailAddress(new MailAddress("administrator@ex2010.local", true));
rule.setConditions(newRules);

// Add Actions and Move the message to a folder
RuleActions newActions = new RuleActions();
newActions.setMoveToFolder("120:AAMkADFjMjNjMmNjLWE3NzgtNGIzNC05OGIyLTAwNTgzNjRhN2EzNgAuAAAAAABbwP+Tkhs0TKx1GMf0D/cPAQD2lptUqri0QqRtJVHwOKJDAAACL5KNAAA=AQAAAA==");
rule.setActions(newActions);
client.createInboxRule(rule);

ルールの更新

Exchange Server 上のルールを更新するには:

  1. IEWSClient クラスを使用して Exchange Server に接続します。
  2. IEWSClient.getInboxRules() メソッドを呼び出してすべてのルールを取得します。
  3. foreach ループで全ルールを巡回し、条件で DisplayName を一致させて変更したいルールを取得します。
  4. ルールのプロパティを更新する
  5. IEWSClient.updateInboxRule() メソッドを呼び出してルールを更新します。

以下のコードスニペットは、ルールを更新する方法を示しています。

IEWSClient client = EWSClient.getEWSClient(mailboxURI, credential);

System.out.println("Connected to Exchange server");

// Get all Inbox Rules
InboxRule[] inboxRules = client.getInboxRules();

// Loop through each rule
for (InboxRule inboxRule : inboxRules) {
    System.out.println("Display Name: " + inboxRule.getDisplayName());
    if ("Message from client ABC".equals(inboxRule.getDisplayName())) {
        System.out.println("Updating the rule....");
        inboxRule.getConditions().getFromAddresses().set_Item(0, new MailAddress("administrator@ex2010.local", true));
        client.updateInboxRule(inboxRule);
    }
}