Working with Messages from IMAP Server
Obtaining the identification info for messages received from a mailbox
When retrieving and processing email messages, you can fetch the details of those messages using their sequence numbers. The following features are used to interact with an IMAP mailbox:
-
Aspose.Email.MailboxInfo
class - Represents identification information about message in a mailbox.-
Aspose.Email.MailboxInfo.SequenceNumber
property - The sequence number of a message. -
Aspose.Email.MailboxInfo.UniqueId
property - The unique id of a message.
-
-
Aspose.Email.MailMessage.ItemId
property - Represents identification information about the message in a mailbox.
The code snippet below shows how to obtain identification info about messages:
using (var client = new ImapClient(imapHost, port, emailAddress, password, securityOption))
{
var msgs = client.ListMessages("INBOX").Take(5);
var seqIds = msgs.Select(t => t.SequenceNumber);
var msgsViaFetch = client.FetchMessages(seqIds);
for (var i = 0; i < 5; i++)
{
var thisMsg = msgsViaFetch[i];
Console.WriteLine($"Message ID:{seqIds.ElementAt(i)} SequenceNumber: {thisMsg.ItemId.SequenceNumber} Subject:{thisMsg.Subject}");
}
}
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:
- Create an instance of the ImapClient class.
- Specify a hostname, port, username, and password in the ImapClient constructor.
- Select the folder using SelectFolder() method.
- Call the ListMessages method to get the ImapMessageInfoCollection object.
- 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.
Listing the Message Attachments
To get information about attachments such as name, size without fetching the attachment data, try the following APIs:
- Aspose.Email.Clients.Imap.ImapAttachmentInfo - Represents an attachment information.
- Aspose.Email.Clients.Imap.ImapAttachmentInfoCollection - Represents a collection of the ImapAttachmentInfo class.
- Aspose.Email.Clients.Imap.ListAttachments(int sequenceNumber) - Gets an information for each attachment in a message.
The code sample with steps below will show you how to use the APIs:
-
Call the ListMessages() method on the imapClient object. This method will return an ImapMessageInfoCollection containing information about the messages in the mailbox.
-
Iterate through each message in the messageInfoCollection using a foreach loop.
-
Call the ListAttachments() method on the imapClient object, passing the SequenceNumber property of the message object as a parameter. This method will return an ImapAttachmentInfoCollection containing information about the attachments in the message.
-
Iterate through each attachment in the attachmentInfoCollection using a foreach loop.
-
Within the inner loop, you can access the information about each attachment using properties of the attachmentInfo object. In this example, the name and size of each attachment are logged to the console using
Console.WriteLine()
.Console.WriteLine("Attachment: {0} (size: {1})", attachmentInfo.Name, attachmentInfo.Size);
var messageInfoCollection = imapClient.ListMessages();
foreach (var message in messageInfoCollection)
{
var attachmentInfoCollection = imapClient.ListAttachments(message.SequenceNumber);
foreach (var attachmentInfo in attachmentInfoCollection)
{
Console.WriteLine("Attachment: {0} (size: {1})", attachmentInfo.Name, attachmentInfo.Size);
}
}
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. On the 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.