Exchange Server のルールの操作

ルールの管理

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

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

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

ルールは InboxRule オブジェクトです。その条件は RulePredicates オブジェクト - たとえば送信者や件名に含める語句など - とそれらのアクションを RuleActions オブジェクト - たとえばメッセージを移動するフォルダーです。

本記事のサンプルは以下のヘッダーと名前空間を使用しています:

#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <Clients/Exchange/WebService/EWSClient/EWSClient.h>
#include <Clients/Exchange/WebService/EWSClient/IEWSClient.h>
#include <Clients/Exchange/InboxRules/InboxRule.h>
#include <Clients/Exchange/InboxRules/RulePredicates.h>
#include <Clients/Exchange/InboxRules/RuleActions.h>
#include <system/array.h>
#include <system/collections/specialized/string_collection.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Clients::Exchange;
using namespace Aspose::Email::Clients::Exchange::WebService;

ルールの読み取り

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

  1. 次のものを使用して Exchange Server に接続する IEWSClient クラス。
  2. 呼び出す IEWSClient->GetInboxRules() すべてのルールを取得するメソッド。
  3. ループ内で、すべてのルールを巡回し、条件、アクション、名前などのルールプロパティを表示します。
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");

try
{
    for (SharedPtr<InboxRule> inboxRule : client->GetInboxRules())
    {
        Console::WriteLine(u"Display Name: " + inboxRule->get_DisplayName());

        // The "From Address" condition
        for (SharedPtr<MailAddress> fromAddress : inboxRule->get_Conditions()->get_FromAddresses())
        {
            Console::WriteLine(u"From: " + fromAddress->get_DisplayName() + u" - " + fromAddress->get_Address());
        }

        // The "Subject Contains" condition
        SharedPtr<Collections::Specialized::StringCollection> subjectStrings = inboxRule->get_Conditions()->get_ContainsSubjectStrings();
        for (int32_t i = 0; i < subjectStrings->get_Count(); i++)
        {
            Console::WriteLine(u"Subject contains: " + subjectStrings->idx_get(i));
        }

        // The "Move to Folder" action
        if (!String::IsNullOrEmpty(inboxRule->get_Actions()->get_MoveToFolder()))
        {
            Console::WriteLine(u"Move message to folder: " + inboxRule->get_Actions()->get_MoveToFolder());
        }
    }
}
catch (System::Exception& ex)
{
    Console::WriteLine(ex->get_Message());
}

client->Dispose();

新しいルールの作成

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

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

次のルールは、指定した送信者で件名に "ABC" を含むメッセージを別フォルダーに移動します。フォルダーはクライアントが返すフォルダー情報の一部である ID で識別されます。次の引数は MailAddress コンストラクタ、 true、アドレスの構文チェックをスキップします。

SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");

SharedPtr<InboxRule> rule = MakeObject<InboxRule>();
rule->set_DisplayName(u"Message from client ABC");

// Conditions: the subject contains "ABC" and the message comes from the given sender
SharedPtr<RulePredicates> conditions = MakeObject<RulePredicates>();
conditions->get_ContainsSubjectStrings()->Add(u"ABC");
conditions->get_FromAddresses()->Add(MakeObject<MailAddress>(u"administrator@ex2010.local", true));
rule->set_Conditions(conditions);

// Action: move the message to a folder
SharedPtr<RuleActions> actions = MakeObject<RuleActions>();
actions->set_MoveToFolder(u"120:AAMkADFjMjNjMmNjLWE3NzgtNGIzNC05OGIyLTAwNTgzNjRhN2EzNgAuAAAAAABbwP+Tkhs0TKx1GMf0D/cPAQD2lptUqri0QqRtJVHwOKJDAAACL5KNAAA=AQAAAA==");
rule->set_Actions(actions);

try
{
    client->CreateInboxRule(rule);
}
catch (System::Exception& ex)
{
    Console::WriteLine(ex->get_Message());
}

client->Dispose();

ルールの更新

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

  1. 次のものを使用して Exchange Server に接続する IEWSClient クラス。
  2. 呼び出す IEWSClient->GetInboxRules() すべてのルールを取得するメソッド。
  3. ループ内で全ルールを閲覧し、表示名で変更したいルールを探します。
  4. ルールのプロパティを更新します。
  5. 呼び出す IEWClient->UpdateInboxRule() ルールを更新するメソッド。

サンプルは上記で作成したルールの最初の送信者アドレスを置き換えます。

SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");

for (SharedPtr<InboxRule> inboxRule : client->GetInboxRules())
{
    Console::WriteLine(u"Display Name: " + inboxRule->get_DisplayName());
    if (inboxRule->get_DisplayName() == u"Message from client ABC")
    {
        Console::WriteLine(u"Updating the rule....");
        inboxRule->get_Conditions()->get_FromAddresses()->idx_set(0, MakeObject<MailAddress>(u"administrator@ex2010.local", true));
        client->UpdateInboxRule(inboxRule);
    }
}

client->Dispose();