Работа с Правилами на Exchange Server
Управление Правилами
Aspose.Email можно использовать для управления правилами на Exchange Server с помощью класса EWSClient. Этот класс использует Exchange Web Services (EWS), которые доступны в Exchange Server 2007 и более поздних версиях. Чтобы показать, как управлять правилами, в этой статье объясняется, как:
- Прочитать правила, уже находящиеся на сервере.
- Создать новое правило.
- Обновить существующее правило.
Для всех функций, описанных в этой статье, требуется Microsoft Exchange Server 2010 Service Pack 1.
Чтение Правил
Чтобы получить все правила из Exchange Server:
- Подключитесь к Exchange Server, используя класс IEWSClient.
- Вызовите метод IEWSClient->GetInboxRules() для получения всех правил.
- В цикле просмотрите все правила и отобразите свойства правила, такие как условия, действия и имена.
Следующий фрагмент кода показывает, как читать правила.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C | |
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser()); | |
System::Console::WriteLine(u"Connected to Exchange server"); | |
try | |
{ | |
// Get all Inbox Rules | |
System::ArrayPtr<System::SharedPtr<InboxRule>> inboxRules = client->GetInboxRules(); | |
// Display information about each rule | |
for (System::SharedPtr<InboxRule> inboxRule : inboxRules) | |
{ | |
System::Console::WriteLine(System::String(u"Display Name: ") + inboxRule->get_DisplayName()); | |
// Check if there is a "From Address" condition | |
if (inboxRule->get_Conditions()->get_FromAddresses()->get_Count() > 0) | |
{ | |
for (auto fromAddress : System::IterateOver(inboxRule->get_Conditions()->get_FromAddresses())) | |
{ | |
System::Console::WriteLine(System::String(u"From: ") + fromAddress->get_DisplayName() + u" - " + fromAddress->get_Address()); | |
} | |
} | |
// Check if there is a "Subject Contains" condition | |
if (inboxRule->get_Conditions()->get_ContainsSubjectStrings()->get_Count() > 0) | |
{ | |
for (auto subject : System::IterateOver(inboxRule->get_Conditions()->get_ContainsSubjectStrings())) | |
{ | |
System::Console::WriteLine(System::String(u"Subject contains: ") + subject); | |
} | |
} | |
// Check if there is a "Move to Folder" action | |
if (inboxRule->get_Actions()->get_MoveToFolder().get_Length() > 0) | |
{ | |
System::Console::WriteLine(System::String(u"Move message to folder: ") + inboxRule->get_Actions()->get_MoveToFolder()); | |
} | |
} | |
} | |
catch (System::Exception& ex) | |
{ | |
System::Console::WriteLine(ex.get_Message()); | |
} |
Создание Нового Правила
Чтобы создать новое правило на Exchange Server, выполните следующие шаги:
- Подключитесь к Exchange Server, используя класс IEWSClient.
- Создайте новый экземпляр класса InboxRule и задайте следующие обязательные свойства:
- DisplayName
- Условия
- Действия
- Вызовите метод IEWSClient->CreateInboxRule() для создания правила.
Следующий фрагмент кода показывает, как создать новое правило.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C | |
// Set Exchange Server 2010 web service URL, Username, password, domain information | |
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser()); | |
System::Console::WriteLine(u"Connected to Exchange server"); | |
System::SharedPtr<InboxRule> rule = System::MakeObject<InboxRule>(); | |
rule->set_DisplayName(u"Message from client ABC"); | |
// Add conditions | |
System::SharedPtr<RulePredicates> newRules = System::MakeObject<RulePredicates>(); | |
// Set Subject contains string "ABC" and Add the conditions | |
newRules->get_ContainsSubjectStrings()->Add(u"ABC"); | |
newRules->get_FromAddresses()->Add(System::MakeObject<MailAddress>(u"administrator@ex2010.local", true)); | |
rule->set_Conditions(newRules); | |
// Add Actions and Move the message to a folder | |
System::SharedPtr<RuleActions> newActions = System::MakeObject<RuleActions>(); | |
newActions->set_MoveToFolder(u"120:AAMkADFjMjNjMmNjLWE3NzgtNGIzNC05OGIyLTAwNTgzNjRhN2EzNgAuAAAAAABbwP+Tkhs0TKx1GMf0D/cPAQD2lptUqri0QqRtJVHwOKJDAAACL5KNAAA=AQAAAA=="); | |
rule->set_Actions(newActions); | |
try | |
{ | |
client->CreateInboxRule(rule); | |
} | |
catch (System::Exception& ex) | |
{ | |
System::Console::WriteLine(ex.get_Message()); | |
} |
Обновление Правила
Чтобы обновить правило на Exchange Server:
- Подключитесь к Exchange Server, используя класс IEWSClient.
- Вызовите метод IEWSClient->GetInboxRules() для получения всех правил.
- В цикле просмотрите все правила и получите правило, которое вы хотите изменить, сопоставив DisplayName в условии.
- Обновите свойства правила
- Вызовите метод IEWSClient.UpdateInboxRule() для обновления правила.
Следующий фрагмент кода показывает, как обновить правило.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C | |
// Set mailboxURI, Username, password, domain information | |
System::String mailboxURI = u"https://ex2010/ews/exchange.asmx"; | |
System::String username = u"test.exchange"; | |
System::String password = u"pwd"; | |
System::String domain = u"ex2010.local"; | |
// Connect to the Exchange Server | |
System::SharedPtr<System::Net::NetworkCredential> credential = System::MakeObject<System::Net::NetworkCredential>(username, password, domain); | |
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser()); | |
System::Console::WriteLine(u"Connected to Exchange server"); | |
// Get all Inbox Rules | |
System::ArrayPtr<System::SharedPtr<InboxRule>> inboxRules = client->GetInboxRules(); | |
// Loop through each rule | |
for (System::SharedPtr<InboxRule> inboxRule : inboxRules) | |
{ | |
System::Console::WriteLine(System::String(u"Display Name: ") + inboxRule->get_DisplayName()); | |
if (inboxRule->get_DisplayName() == u"Message from client ABC") | |
{ | |
System::Console::WriteLine(u"Updating the rule...."); | |
inboxRule->get_Conditions()->get_FromAddresses()->idx_set(0, System::MakeObject<MailAddress>(u"administrator@ex2010.local", true)); | |
client->UpdateInboxRule(inboxRule); | |
} | |
} |