Send, Read, and Organize Messages on Exchange Server

Retrieve Exchange Mailbox Information with EWS

Aspose.Email allows you to retrieve mailbox details from Microsoft Exchange using the IEWSClient class. By calling the GetMailboxInfo() method, the client returns an ExchangeMailboxInfo object containing useful folder URIs such as Mailbox, Inbox, Drafts, and Sent Items.

To connect to the Exchange Server with Exchange Web Services (EWS), use the IEWSClient class. This class uses EWS to connect to and manage items on an Exchange Server.

The following code snippet demonstrates how to get mailbox information using the exchange web services.

Send Email Messages via EWS

You can send emails through Exchange by calling the IEWSClient->Send() method. It takes a MailMessage object and submits it directly through the server using EWS.

The following code sample demonstrates how to send an HTML email message through an Exchange Server using Exchange Web Services (EWS) with Aspose.Email for C++. It shows the complete process of establishing a connection to Exchange, creating a mail message with sender, recipient, subject, and HTML content, and then sending the message using the EWS client’s Send method.

Reading Emails from Other User’s Mailbox

Some accounts on Exchange Servers have the right to access multiple mailboxes, and some users have multiple email accounts on the same Exchange Server. In both cases, users can access other users’ mailboxes with Aspose.Email. The API provides a mechanism for accessing folders and emails from other mailboxes using the IEWSClient class. This functionality can be achieved using the overloaded GetMailboxInfo() method and providing the user email address as a parameter.

The following code snippet shows you how to read emails using the IEWSClient class.

List Messages Using EWS

Aspose.Email for C++ allows you to retrieve message metadata from Exchange Server mailboxes through the IEWSClient. Using the EWS-based ListMessages API, you can list messages from any folder, browse message metadata, and implement paging for large mailboxes.

List Messages from the Inbox

Use ListMessages to retrieve basic message information such as subject, sender, recipients, and message ID from the Inbox or any folder.

The following code sample demonstrates how to list and display basic information for all messages in an Exchange Server.

  1. Create an instance of IEWSClient.
  2. Call ListMessages with the target folder URI.
  3. Iterate through ExchangeMessageInfoCollection.

List Messages from Any Folder

ListMessages accepts any valid folder URI, allowing you to list items from Deleted Items, Drafts, Sent Items, or custom folders. Use the IEWSClient->get_MailboxInfo->xxxFolderUri property to get the URI of different folders.

The following code sample demonstrates how to access different Exchange Server folder URIs and retrieve messages from a specified folder.

Pagination in Message Listing

For large mailboxes, use ListMessagesByPage to load messages in smaller blocks.

The following code sample demonstrates how to implement pagination for retrieving large numbers of messages from an Exchange Server inbox.

  1. First, it creates multiple test messages on the server.
  2. Then, uses the ListMessagesByPage method to retrieve messages in smaller batches (5 messages per page in this case), iterating through all pages until the last page is reached.
  3. Finally, the code verifies that all messages were successfully retrieved by counting the total items across all pages.

Get Message Type Information

Use ExchangeMessageInfo->MessageInfoType to determine the underlying Exchange message type (e.g., email, meeting request, etc.).

The following code sample demonstrates how to connect to an Exchange Server and retrieve message type information from the Deleted Items folder.

Save Messages Using Exchange Web Services (EWS)

Aspose.Email enables you to retrieve messages from an Exchange Server mailbox and save them in multiple formats, such as EML, memory streams, and MSG. The examples below demonstrate how to fetch message information and store messages using the IEWSClient API.

Save Messages as EML Files

To save mailbox messages as EML files:

  1. Create an IEWSClient instance using valid credentials.
  2. Call ListMessages() to retrieve an ExchangeMessagesInfoCollection.
  3. Loop through the collection to access each message unique URI.
  4. Call SaveMessage() to store each message on disk in EML format.

The following code sample demonstrates how to save email messages from an Exchange Server inbox as individual EML files locally using Aspose.Email for C++.

Save Messages to a Memory Stream

Instead of writing to disk, you can save messages to a memory stream—useful for storing emails in a database or processing them in memory.

The following code sample demonstrates how to save email messages from an Exchange Server inbox into memory streams.

Save Messages in MSG Format

To save messages as MSG:

  1. Retrieve the message using FetchMessage(), which returns a MailMessage.
  2. Call MailMessage::Save() with MSG save options.

The following code sample demonstrates how to fetch and save email messages from an Exchange Server inbox as Outlook MSG format files.

Retrieve Message Details by Message URI

When only a message unique URI is available, you can still retrieve full ExchangeMessageInfo objects. The IEWSClient::ListMessages() overload accepts a list of message IDs (URIs) and returns an ExchangeMessageInfoCollection. Use this feature when you store or receive message URIs externally and need to load metadata (subject, sender, size, etc.) without fetching full messages.

The following code sample demonstrates how to create multiple email messages on an Exchange Server and then retrieve their message information using unique identifiers.

Fetch Full Message Content

ListMessages() returns summary information (subject, sender, IDs). To load full message content — body, headers, attachments — use FetchMessage().

The following code sample demonstrates how to fetch complete messages from an Exchange Server inbox and extract attachment information:

  1. Create an IEWSClient instance.
  2. Call ListMessages() to get basic message metadata.
  3. Extract each message UniqueUri.
  4. Call FetchMessage() to retrieve full message details.

Fetch Message Size (Without Downloading Full Message)

Aspose.Email provides message size information without fetching the entire email, through the ExchangeMessageInfo::Size property.

This is useful for:

  • mailbox statistics
  • quota calculations
  • filtering large messages before downloading

The following code sample demonstrates how to list and display message metadata including size from an Exchange Server inbox.

Download Emails from Exchange Public Folders

Exchange public folders allow storing shared messages across users.

Aspose.Email IEWSClient allows you to perform the following operations wih these folders and messages stored in them:

  • List public folders
  • Browse subfolders recursively
  • Download messages and save them (e.g., as MSG files)

Note: Microsoft Exchange Server 2007 or later is required, as earlier versions do not support EWS.

The following code sample demonstrates how to download messages from all public folders and their subfolders on an Exchange Server recursively, and save them as Outlook MSG files locally.

Move Messages Between Exchange Folders

You can move messages from one Exchange folder to another using the IEWSClient::MoveItem method. It requires:

  • The unique URI of the message to move.
  • The destination folder unique URI.

The following code sample demonstrates how to filter and move specific email messages from an Exchange Server inbox to another folder based on content criteria.

  1. Connect to Exchange Server using EWS credentials.
  2. Get mailbox information (folder URIs).
  3. List all messages from the Inbox.
  4. Iterate through each message.
  5. Check if subject contains “process this message”.
  6. Move matching messages to Deleted Items folder.
  7. Output confirmation for each moved message.

Delete Messages from an Exchange Folder

You can delete email messages from a folder with the help of the IEWSClient->DeleteMessage method. It takes the message’s unique URI as a parameter.

The following code sample demonstrates how to filter and delete specific email messages from an Exchange Server inbox based on subject criteria.

  1. Iterate through Inbox messages.
  2. Process messages based on some criteria (in this example, we find a keyword in the message subject).
  3. Delete matching messages.

Copy Messages to Another Exchange Folder

Use IEWSClient::CopyItem to duplicate a message into a different folder. The overloaded version returns the URI of the newly created copy.

The following code sample demonstrates how to create an email message on an Exchange Server and copy it to another folder.