Exchange Server에서 규칙 작업
Contents
[
Hide
]
규칙 관리
Aspose.Email는 Exchange Server에서 규칙을 관리하는 데 사용할 수 있습니다. EWSClient 클래스. 이 클래스는 Exchange Server 2007 및 이후 릴리스에서 사용할 수 있는 Exchange Web Services (EWS)를 사용합니다. 규칙 관리 방법을 보여주기 위해 이 문서는 다음을 설명합니다:
- 서버에 이미 존재하는 규칙을 읽습니다.
- 새 규칙 생성.
- 기존 규칙 업데이트.
이 문서에 설명된 모든 기능에는 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"를 포함하는 제목인 메시지를 다른 폴더로 이동합니다. 폴더는 클라이언트가 반환한 폴더 정보의 일부인 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에서 규칙을 업데이트하려면:
- 다음 클래스를 사용하여 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();