Working with Messages from IMAP Server

Listing MIME Message IDs from Server

ImapMessageInfo provides the MIME MessageId for message identification without extracting the complete message. The following code snippet shows you how to list MIME messageId.

Listing Messages from Server

Aspose.Email provides a 2-member overloaded variant of ListMessages() to retrieve a specified number of messages based on a query. The following code snippet shows you how to list Messages.

Listing Messages from Server Recursively

The IMAP protocol supports listing messages recursively from a mailbox folder. This helps list messages from subfolders of a folder as well. The following code snippet shows you how to list messages recursively.

Listing Messages with MultiConnection

ImapClient provides a UseMultiConnection property which can be used to create multiple connections for heavy operations. You may also set the number of connections to be used during multiconnection mode by using ImapClient.ConnectionsQuantity. The following code snippet demonstrates the use of the multiconnection mode for listing messages and compares its performance with single connection mode.

Get Messages in descending order

Aspose.Email provides ImapClient.ListMessagesByPage method which lists messages with paging support.Some overloads of ImapClient.ListMessagesByPage accept PageSettings as a parameter. PageSettings provides an AscendingSorting property which when set to false, returns emails in descending order.

The following example code demonstrates the use of AscendingSorting property of the PageSettings class to change the order of emails.

Fetch Messages from Server and Save to disc

The ImapClient class can fetch messages from an IMAP server and save the messages in EML format to a local disk. The following steps are required to save the messages to disk:

  1. Create an instance of the ImapClient class.
  2. Specify a hostname, port, username, and password in the ImapClient constructor.
  3. Select the folder using SelectFolder() method.
  4. Call the ListMessages method to get the ImapMessageInfoCollection object.
  5. Iterate through the ImapMessageInfoCollection collection, call the SaveMessage() method and provide the output path and file name.

The following code snippet shows you how to fetch email messages from a server and save them.

Saving Messages in MSG Format

In the above example, the emails are saved in EML format. To save emails in MSG format, the ImapClient.FetchMessage() method needs to be called. It returns the message in an instance of the MailMessage class. The MailMessage.Save() method can then be called to save the message to MSG. The following code snippet shows you how to save messages in MSG Format.

Group Fetch Messages

ImapClient provides a FetchMessages method which accepts iterable of Sequence Numbers or Unique ID and returns a list of MailMessage. The following code snippet demonstrates the use of the FetchMessages method to fetch messages by Sequence Numbers and Unique ID.

Listing Messages with Paging Support

In scenarios, where the email server contains a large number of messages in the mailbox, it is often desired to list or retrieve the messages with paging support. Aspose.Email API’s ImapClient lets you retrieve the messages from the server with paging support.

Getting Folders and Reading Messages Recursively

In this article, most of the ImapClient features are used to create an application that lists all the folders and sub-folders recursively from an IMAP server. It also saves the messages in each folder and sub-folder in MSG format on a local disk. In disk, folders and messages are created and saved in the same hierarchical structure as on the IMAP server. The following code snippet shows you how to get the messages and sub-folders information recursively.

Retrieving Extra Parameters as Summary Information

Getting List-Unsubscribe Header Information

List-Unsubscribe header contains the URL for unsubscribing from email lists e.g. advertisements, newsletters, etc. To get the List-Unsubscribe header, use the ListUnsubscribe property of the ImapMessageInfo class. The following example shows the use of the ListUnsubscribe property to get the List-Unsubscribe header.