在 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 标识,该 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. 调用 IEWSClient->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();