Manage Exchange Conversations via EWS

Aspose.Email allows you to manage conversation items on Microsoft Exchange Server using the EWSClient class. This functionality relies on Exchange Web Services (EWS), available in Exchange Server 2007 and later. The examples in this guide demonstrate how to find, copy, move, and delete conversation items on Exchange Server 2010 SP1 or later.

A conversation groups the messages of one thread - the original message and all replies and forwards - across the folders of the mailbox. The operations below act on all messages of a conversation at once and identify it by its conversation ID.

The samples in this article use these headers and namespaces:

#include <Clients/Exchange/WebService/EWSClient/EWSClient.h>
#include <Clients/Exchange/WebService/EWSClient/IEWSClient.h>
#include <Clients/Exchange/WebService/Conversation/ExchangeConversation.h>
#include <Clients/Exchange/WebService/Conversation/ExchangeConversationFlagStatus.h>
#include <Clients/Exchange/ExchangeMailboxInfo.h>
#include <system/array.h>
#include <system/object_ext.h>

using namespace System;
using namespace Aspose::Email::Clients::Exchange;
using namespace Aspose::Email::Clients::Exchange::WebService;

Find Conversations

To retrieve conversation information from a specific Exchange folder, follow the steps below:

  1. Connect to the Exchange Server using IEWSClient.
  2. Call FindConversations() method to list conversations in the target folder.
  3. Read properties of each ExchangeConversation such as the conversation ID, topic, and flag status.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");

ArrayPtr<SharedPtr<ExchangeConversation>> conversations = client->FindConversations(client->get_MailboxInfo()->get_InboxUri());
for (SharedPtr<ExchangeConversation> conversation : conversations)
{
    Console::WriteLine(u"Topic: " + conversation->get_ConversationTopic());
    Console::WriteLine(u"Id: " + conversation->get_ConversationId());
    Console::WriteLine(u"Flag status: " + ObjectExt::ToString(conversation->get_FlagStatus()));
}

client->Dispose();

Copy Conversations

To copy conversation items to another folder:

  1. Connect to the Exchange Server using IEWSClient.
  2. Retrieve conversations with the FindConversations() method.
  3. Use CopyConversationItems() to copy matching conversations into the destination folder.

The sample copies the conversations whose topic contains “test email” into the Deleted Items folder.

SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");

for (SharedPtr<ExchangeConversation> conversation : client->FindConversations(client->get_MailboxInfo()->get_InboxUri()))
{
    if (conversation->get_ConversationTopic().Contains(u"test email"))
    {
        client->CopyConversationItems(conversation->get_ConversationId(), client->get_MailboxInfo()->get_DeletedItemsUri());
        Console::WriteLine(u"Copied: " + conversation->get_ConversationTopic());
    }
}

client->Dispose();

Move Conversations

You can move conversations between folders.

  1. Connect to the Exchange Server using IEWSClient.
  2. Identify the conversations to move.
  3. Call the MoveConversationItems() method with the destination folder URI.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");

try
{
    for (SharedPtr<ExchangeConversation> conversation : client->FindConversations(client->get_MailboxInfo()->get_InboxUri()))
    {
        if (conversation->get_ConversationTopic().Contains(u"test email"))
        {
            client->MoveConversationItems(conversation->get_ConversationId(), client->get_MailboxInfo()->get_DeletedItemsUri());
            Console::WriteLine(u"Moved: " + conversation->get_ConversationTopic());
        }
    }
}
catch (System::Exception& ex)
{
    Console::WriteLine(ex->get_Message());
}

client->Dispose();

Delete Conversations

To delete conversation threads from a folder:

  1. Connect to the Exchange Server using IEWSClient.
  2. Use FindConversations() to locate the target conversations.
  3. Call the DeleteConversationItems() method to remove them.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");

try
{
    for (SharedPtr<ExchangeConversation> conversation : client->FindConversations(client->get_MailboxInfo()->get_InboxUri()))
    {
        if (conversation->get_ConversationTopic().Contains(u"test email"))
        {
            client->DeleteConversationItems(conversation->get_ConversationId());
            Console::WriteLine(u"Deleted: " + conversation->get_ConversationTopic());
        }
    }
}
catch (System::Exception& ex)
{
    Console::WriteLine(ex->get_Message());
}

client->Dispose();