Working with Exchange Mailbox and Messages
Getting Mailbox Information Using EWS
You can get mailbox information from an Exchange Server by calling the GetMailboxInfo method of the IEWSClient class. It returns an instance of type ExchangeMailboxInfo. Get mailbox information from properties such as MailboxUri, InboxUri, and DraftsUri. This article shows how to access mailbox information using Exchange Web Services.
To connect to the Exchange Server using 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 shows you how to get mailbox information using the exchange web services.
Sending Email Messages
You can send email messages using an Exchange Server with the IEWSClient->Send() method accepts a MailMessage instance as a parameter and sends the email. This article explains how to send email messages using Exchange Web Services.
The following code snippet shows you how to sends email messages using IEWSClient.
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 user’s mailboxes using Aspose.Email. This 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.
Listing Messages
A list of the email messages in an Exchange mailbox can be fetched by calling the ListMessages method. Get the basic information about messages, such as subject, from, to and message ID, using the ListMessages method.
Simple Messages Listing
To list the messages in an Exchange mailbox:
- Create an instance of the IEWSClient class.
- Call the ListMessages method to get the message collection.
- Loop through the collection and display message information.
The following code snippet shows you how to connect to an exchange server using EWS and lists messages from the inbox folder.
Listing Messages From Different Folders
The above code snippet list all the messages in the Inbox folder. It is possible to get the list of messages from other folders as well. The ListMessages method accepts a folder URI as a parameter. As long as the folder URI is valid, you can get the list of messages from that folder. Use the IEWSClient->get_MailboxInfo->xxxFolderUri property to get the URI of different folders. The rest of the code is the same as for getting a list of messages. The following code snippet shows you how to list messages from different folders using EWS.
Listing Messages with Paging Support
The following code snippet shows you how to get a list of messages with paging support.
Getting Message Type Information from ExchangeMessageInfo
Saving Messages
This article shows how to get messages from an Exchange Server mailbox and save them to disk in EML and MSG formats:
- Save as EML on disk.
- Save to memory stream.
- Save as MSG.
Saving Messages to EML
To get messages and save in EML format:
- Create an instance of the IEWSClient class.
- Provide the mailboxUri, username, password and domain.
- Call the IEWSClient->ListMessages() method to get an instance of the ExchangeMessagesInfoCollection collection.
- Loop through the ExchangeMessagesInfoCollection collection to get the unique URI for each message.
- Call the IEWSClient->SaveMessage() method and pass the unique URI and save location as parameters.
The following code snippet shows you how to use EWS to connect to the Exchange Server and save messages as EML files.
Saving Messages to a Memory Stream
Instead of saving EML files to disk, it is possible to save it to a memory stream. This is useful when you want to save the stream to some storage location like a database. Once the stream has been saved to a database, you can reload the EML file into the MailMessage class. The following code snippet shows you how to save messages from an Exchange Server mailbox to a memory stream using EWS.
Saving Messages in MSG Format
The IEWSClient->SaveMessage() method can directly save the message to EML format. To save the messages to MSG format, first, call the IEWSClient->FetchMessage() method which returns an instance of the MailMessage class. Then call the MailMessage->Save() method to save the message to MSG. The following code snippet shows you how to get messages from an Exchange Server mailbox and saves them to MSG format using EWS.
Getting ExchangeMessageInfo from Message URI
An email message is represented by its unique identifier, URI, and is an integral part of the ExchangeMessageInfo object. In case, only message URI is available, then ExchangeMessageInfo object can also be retrieved using this available information. The overload version of ListMessages takes a list of Ids and returns an ExchangeMessageInfoCollection collection. The following code snippet shows you how to get ExchangeMessageInfo from message URI.
Fetch Messages from an Exchange Server Mailbox
The ListMessages() method is used to get a list of messages from an Exchange Server mailbox. The ListMessages() method gets basic information about messages, for example, the subject, the message ID, from, and to. To get the complete message details, Aspose.Email provides the IEWSClient->FetchMessage() method. This method accepts the message URI as a parameter and returns an instance of the MailMessage class. The MailMessage class then provides message details like the body, headers, and attachments. To fetch messages from Exchange Server Mailbox:
- Create an instance of type IEWSClient.
- Specify the server name, user name, password, and domain.
- Call IEWSClient->ListMessages() method to get the ExchangeMessagesInfoCollection.
- Loop through the ExchangeMessagesInfoCollection collection to get ExchangeMessageInfo->get_UniqueUri() values.
- Call IEWSClient->FetchMessage() and pass ExchangeMessageInfo->get_UniqueUri() as parameter.
The following code snippet demonstrates fetching all the messages using EWS.
Pre-Fetch Message Size
Microsoft Outlook InterOp provides the feature of retrieving message size before actually fetching the complete message from the server. In case of Aspose.Email API, the summary information retrieved from the Exchange server is represented by the ExchangeMessageInfo class. It provides the feature of retrieving message size by using the Size property. In order to retrieve the message size, the standard call to IEWSClient->ListMessages() is used to retrieve the ExchangeMessagesInfoCollection. The following code snippet shows you how to display message size using the ExchangeMessageInfo class.
Download Messages from Public Folders
Microsoft Exchange Server lets users create public folders and post messages in them. To do this through your application, use Aspose.Email’s EWSClient class to connect to the Exchange Server and read and download messages and posts from public folders. The following code snippet shows you how to reads all public folders and subfolders, and list and download any messages found in these folders. This example only works with Microsoft Exchange Server 2007 or above since only these support EWS.
Moving Messages
You can move email messages from one folder to another with the help of the IEWSClient class Move method. It takes the following parameters:
- The unique URI of the message which is to be moved.
- The unique URI of the destination folder.
Moving Messages between Folders
The following code snippet shows you how to move a message in a mailbox from the Inbox folder to a folder called Processed. In this example, the application:
- Reads messages from the Inbox folder.
- Processes some of the messages based on some criteria (in this example, we find a keyword in the message subject).
- Moves messages which fulfill the given condition to the processed folder.
Deleting Messages
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 snippet shows you how to delete a message from the Inbox folder. For the purpose of this example, the code:
- Reads messages from the Inbox folder.
- Process messages based on some criteria (in this example, we find a keyword in the message subject).
- Deletes the message.
Copying Messages
Aspose.Email API allows copying a message from one folder to another folder using the IEWSClient->CopyItem method. The overloaded version of this method returns the Unique URI of the copied message as shown in this article.
Copying a Message to Another Folder
The following code snippet shows you how to copy a message to another folder.