Lavorare con le regole su Exchange Server

Gestione delle regole

Aspose.Email può essere usato per gestire le regole su Exchange Server utilizzando il EWSClient classe. Questa classe utilizza Exchange Web Services (EWS), disponibili in Exchange Server 2007 e versioni successive. Per mostrare come gestire le regole, questo articolo spiega come:

  • Leggi le regole già presenti sul server.
  • Crea una nuova regola.
  • Aggiorna una regola esistente.

Microsoft Exchange Server 2010 Service Pack 1 è richiesto per tutte le funzionalità descritte in questo articolo.

Una regola è rappresentata da una InboxRule oggetto. Le sue condizioni sono conservate in un RulePredicates oggetto - ad esempio i mittenti o le parole che l’oggetto deve contenere - e le sue azioni in un RuleActions oggetto - ad esempio la cartella in cui spostare il messaggio.

I campioni di questo articolo usano queste intestazioni e spazi dei nomi:

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

Leggi le regole

Per ottenere tutte le regole dal server Exchange:

  1. Connettiti a un Exchange Server usando il IEWSClient classe.
  2. Chiama il IEWSClient->GetInboxRules() metodo per ottenere tutte le regole.
  3. In un ciclo, scorre tutti i regole e visualizza le proprietà della regola come condizioni, azioni e nomi.
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();

Creazione di una nuova regola

Per creare una nuova regola sul server Exchange, esegui i seguenti passaggi:

  1. Connettiti a un Exchange Server usando il IEWSClient classe.
  2. Crea una nuova istanza della InboxRule classe e impostare le seguenti proprietà obbligatorie:
    1. Nome visualizzato
    2. Condizioni
    3. Azioni
  3. Chiama il IEWSClient->CreateInboxRule() metodo per creare la regola.

La regola qui sotto sposta i messaggi da un determinato mittente il cui oggetto contiene "ABC" in un’altra cartella. La cartella è identificata dal suo ID, che fa parte delle informazioni della cartella restituite dal client. Il secondo argomento del MailAddress costruttore, true, salta il controllo sintattico dell’indirizzo.

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

Aggiornamento di una regola

Per aggiornare una regola sul server Exchange:

  1. Connettiti a un Exchange Server usando il IEWSClient classe.
  2. Chiama il IEWSClient->GetInboxRules() metodo per ottenere tutte le regole.
  3. In un ciclo, sfoglia tutte le regole e trova quella che vuoi modificare per nome visualizzato.
  4. Aggiorna le proprietà della regola.
  5. Chiama il IEWSClient->UpdateInboxRule() metodo per aggiornare la regola.

Il campione sostituisce il primo indirizzo del mittente della regola creata sopra.

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