Working with Exchange Mailbox and Messages

Getting Mailbox Information

If you want to connect to the Exchange Server using Exchange Web Services (EWS), use the ExchangeWebServiceClient class in the Aspose.Email.Exchange namespace. This class uses EWS to connect to and manage items on an Exchange Server.

The code snippets below show how to get mailbox information using the Exchange Web Services.

Listing Messages

List Exchange Server Messages using EWS

The code snippets below connects to an Exchange Server using EWS and lists messages from the Inbox folder:

Listing Messages from Different Folders with EWS

Alternatively, if you want to list messages using Exchange Web Services, use the ExchangeWebService class instead of the ExchangeClient class. The code snippets below use EWS:

Listing Messages with Paging Support

Aspose.Email provides the capability to fetch messages from Exchange server based on multiple server responses.

Listing Messages by Extended Properties

In order to list messages having certain extended properties, the overloaded method of ListMessages can be used as shown in code sample below.


 IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "Shabir.haider@studentpartner.com", "LoveAir1993");

{

    try

    {

        List<String> extendedProperties = new List<String>();

        extendedProperties.add("blablaprop");

        ExchangeMessageInfoCollection messageInfoCol = client.listMessages(

                client.getMailboxInfo().getInboxUri(),

                ExchangeListMessagesOptions.FetchAttachmentInformation,

                extendedProperties);

    }

    finally

    {

    }

}

Getting Message Class Information from ExchangeMessageInfo

Reading Emails from Other User’s Mailbox with EWS

Send Email Message using EWS

You can send email messages using an Exchange Server with the help of the tools in com.aspose.email. The IEWSClient.send() method accepts a MailMessage instance as a parameter and sends the email. Aspose.Email provides the IEWSClient class to connect to Microsoft Exchange Server using Exchange Web Services. The code snippets that follow uses EWS to send emails using Microsoft Exchange Server.

Get ExchangeMessageInfo From Message URI

Pre-Fetch Message Size using Aspose.Email IEWSClient

In order to retrieve the message size, the standard call to IEWClient’s ListMessages is used that retrieves collection of ExchangeMessagInfo. The following sample code provides an example of displaying message size using the ExchangeMessageInfo class.

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 Messages from Exchange Server Mailbox to EML

To save messages using Exchange Web Services, use the ExchangeWebServiceClient class can be used. The following code snippets use EWS to connect to the Exchange Server and save messages as EML files.

Save Messages to an OutputStream

Instead of saving EML files to disk, it is possible to save it to an OutputStream. 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 code snippets below save messages from an Exchange Server mailbox to a memory stream. When using Exchange Web Services, the following code can be used to save the message to a stream.

Save Messages in MSG Format

Messages can also be saved in MSG format using EWS using the ExchangeWebServiceClient class, which is part of the com.aspose.email namespace. The following code snippets save the messages to MSG on disk using EWS.

Moving Messages between Folders using EWS

You can move email messages from one folder to another with the help of the IEWSClient.moveItem() method. It takes the parameters:

  • The unique URI of the message which is to be moved.
  • The unique URI of the destination folder.

The sample code below moves a message in a mailbox from the Inbox folder to a folder called Processed. In this example, the application:

  1. Reads messages from the Inbox folder.
  2. Processes some of the messages based on some criteria (in this example, we find a keyword in the message subject).
  3. Moves messages which fulfill the given condition to the Processed folder.  

Aspose.Email provides the ExchangeWebServiceClient class to connect to Microsoft Exchange Server using Exchange Web Services. The code snippets that follow use Exchange Web Services to move messages from the Inbox folder to the Processed folder.

Copy Messages From One Folder to Other using EWS

Deleting Messages from Exchange Server

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 sample code below deletes a message from the Inbox folder. For the purpose of this example, the code:

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