การทำงานกับกฎบน 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. เรียกใช้ 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();