Working with Exchange Mailbox and Messages using WebDav
Getting Mailbox Information using WebDav
The ExchangeClient class has members that can be used to get mailbox information from an Exchange Server by calling the ExchangeClient.GetMailboxInfo() method. 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 directly from an Exchange Server.
To get the Exchange Mailbox information:
- Create an instance of the ExchangeClient class.
- Specify the Exchange Server, username, password and domain in the ExchangeClient constructor.
- Call the ExchangeClient.GetMailboxSize() method to get mailbox size.
- Call the ExchangeClient.GetMailboxInfo() method to get an instance of the ExchangeMailboxInfo class.
- Get the mailbox information using the ExchangeMailboxInfo class' different properties.
The following code snippet shows you how to get exchange mailbox information.
Sending Email Messages
You can send email messages using an Exchange Server with the help of the tools in Aspose.Email.Exchange. The ExchangeClient.Send() method accepts a MailMessage instance as a parameter and sends the email. This article explains how to send email messages using an Exchange server.
Sending Email Messages Using Exchange Server
To send emails using an Exchange Server:
- Create an instance of the ExchangeClient class.
- Specify server name, username, password, and domain.
- Create an instance of the MailMessage class.
- Specify the from, to, subject and other MailMessage properties.
- Call the ExchangeClient.Send() method to send the email.
The following code snippet shows you how to sends email messages using Exchange Server.
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 for .NET. This API provides a mechanism for accessing folders and emails from other mailboxes using the ExchangeClient 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 use the ExchangeClient class to access another mailbox.
Listing Messages
A list of the email messages in an Exchange mailbox can be fetched by calling the ExchangeClient.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 ExchangeClient class.
- Call the ListMessages method and create a message collection.
- Loop through the collection and display message information.
The following code snippet shows you how to connects to the Exchange mailbox and get a list of messages from the inbox folder.
Listing Messages From Different Folders
The above code snippets list all the messages in the Inbox folder. It is possible to get the list of messages from other folders as well. The ExchangeClient.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 ExchangeClient.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 ExchangeClient.
Listing Messages by ID
The above code snippet used the ListMessages method to list all the messages in a specified Exchange Server mailbox folder. If you know the message ID of a message beforehand, you can get the message using the ListMessagesbyId() method. To get the message, pass the folder URI and message ID. This scenario is useful when you already know the message ID, for example, when message IDs are saved to the database. The following code snippet shows you how to list messages by ID.
Saving Messages
This topic 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 ExchangeClient class.
- Provide the server name, username, password, and domain.
- Call the ExchangeClient.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 ExchangeClient.SaveMessage() method and pass the unique URI as a parameter.
- Provide the SaveMessage() method with a path to where you want to save the file.
The following code snippet shows you how to save messages to EML.
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.
Saving Messages in MSG Format
The ExchangeClient.SaveMessage() method can directly save the message to EML format. To save the messages to MSG format, first, call the ExchangeClient.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 gets messages from an Exchange Server mailbox and saves them to MSG format.
Fetch Messages from an Exchange Server Mailbox
Listing Messages on an Exchange Server used the ListMessages method 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.Exchange provides the ExchangeClient.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 body, headers, and attachments. To fetch messages from Exchange Server Mailbox:
- Create an instance of type ExchangeClient.
- Specify the mailboxUri, user name, password, and domain.
- Call ListMessages to get the ExchangeMessagesInfoCollection.
- Loop through the ExchangeMessagesInfoCollection collection to get ExchangeMessageInfo.UniqueURI values.
- Call ExchangeClient.FetchMessage() and pass ExchangeMessageInfo.UniqueURI as a parameter.
The following code snippet shows you connect to the Exchange Server mailbox and fetch all the messages.
Pre-Fetch Message Size
Microsoft Outlook InterOp provides the feature of retrieving message size before actually fetching the complete message from the server. In the case of Aspose.Email API, the summary information retrieved from the Exchange server is represented by ExchangeMessageInfo class. It provides the same feature of retrieving message size using the Size property. In order to retrieve the message size, the standard call to ExchangeClient’s ListMessages is used that retrieves collection of ExchangeMessageInfo. The following code snippet shows you how to display message size using the ExchangeMessageInfo class.
Moving Messages
You can move email messages from one folder to another with the help of the ExchangeClient class MoveItems method. It takes the 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 ExchangeClient class DeleteMessage method. It takes the message’s unique URI as a parameter.
Deleting Messages from an Exchange Server
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).
- Delete the message.