Bekerja dengan Aturan di Exchange Server

Mengelola Aturan

Aspose.Email dapat digunakan untuk mengelola aturan di Exchange Server menggunakan EWSClient kelas. Kelas ini menggunakan Exchange Web Services (EWS), yang tersedia di Exchange Server 2007 dan rilis selanjutnya. Untuk menunjukkan cara mengelola aturan, artikel ini menjelaskan cara:

  • Baca aturan yang sudah ada di server.
  • Buat aturan baru.
  • Perbarui aturan yang ada.

Microsoft Exchange Server 2010 Service Pack 1 diperlukan untuk semua fitur yang dijelaskan dalam artikel ini.

Sebuah aturan direpresentasikan oleh sebuah InboxRule objek. Kondisinya disimpan dalam sebuah RulePredicates objek — misalnya pengirim atau kata yang harus ada di subjek — dan aksinya dalam sebuah RuleActions objek — misalnya folder tujuan pemindahan pesan.

Contoh dalam artikel ini menggunakan header dan namespace berikut:

#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;

Baca Aturan

Untuk mengambil semua aturan dari Exchange Server:

  1. Terhubung ke Server Exchange menggunakan IEWSClient kelas.
  2. Panggil IEWSClient->GetInboxRules() metode untuk mendapatkan semua aturan.
  3. Dalam sebuah loop, telusuri semua aturan dan tampilkan properti aturan seperti kondisi, tindakan, dan nama.
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();

Membuat Aturan Baru

Untuk membuat aturan baru di Exchange Server, ikuti langkah-langkah berikut:

  1. Terhubung ke Server Exchange menggunakan IEWSClient kelas.
  2. Buat instance baru dari InboxRule kelas dan atur properti wajib berikut:
    1. Nama Tampilan
    2. Kondisi
    3. Tindakan
  3. Panggil IEWSClient->CreateInboxRule() metode untuk membuat aturan.

Aturan di bawah memindahkan pesan dari pengirim tertentu yang subjeknya berisi "ABC" ke folder lain. Folder diidentifikasi oleh ID‑nya, yang merupakan bagian dari informasi folder yang dikembalikan klien. Argumen kedua dari MailAddress konstruktor, true, melewati pemeriksaan sintaks alamat.

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();

Memperbarui Aturan

Untuk memperbarui aturan di Exchange Server:

  1. Terhubung ke Server Exchange menggunakan IEWSClient kelas.
  2. Panggil IEWSClient->GetInboxRules() metode untuk mendapatkan semua aturan.
  3. Dalam sebuah loop, telusuri semua aturan dan temukan aturan yang ingin diubah berdasarkan nama tampilannya.
  4. Perbarui properti aturan.
  5. Panggil IEWSClient->UpdateInboxRule() metode untuk memperbarui aturan.

Contoh mengganti alamat pengirim pertama dari aturan yang dibuat di atas.

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();