עבודה עם כללים ב‑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:
- התחבר ל‑Exchange Server באמצעות ה‑ IEWSClient מחלקה.
- הפעל את IEWSClient->GetInboxRules() שיטה לקבל את כל הכללים.
- בלולאה, עבור בכל הכללים והצג את מאפייני הכלל כגון תנאים, פעולות ושמות.
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, בצע את הצעדים הבאים:
- התחבר ל‑Exchange Server באמצעות ה‑ IEWSClient מחלקה.
- צור מופע חדש של ה‑ InboxRule מחלקה והגדר את המאפיינים החיוניים הבאים:
- שם תצוגה
- תנאים
- פעולות
- הפעל את IEWSClient->CreateInboxRule() שיטה ליצירת כלל.
הכלל שלמטה מעביר את ההודעות ממשתמש נתון שהנושא שלהם מכיל "ABC" לתיקייה אחרת. התיקייה מזוהה לפי המזהה שלה, שהוא חלק ממידע התיקייה שהלקוח מחזיר. הארגומנט השני של ה‑ 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:
- התחבר ל‑Exchange Server באמצעות ה‑ IEWSClient מחלקה.
- הפעל את IEWSClient->GetInboxRules() שיטה לקבל את כל הכללים.
- בלולאה, עבור דרך כל הכללים ומצא את הכלל שברצונך לשנות לפי שם התצוגה שלו.
- עדכן את המאפיינים של החוק.
- הפעל את 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();