Retrieve and List Emails from IMAP Server
Retrieving and Listing Messages
How to Obtain Identification Information for Messages in a Mailbox
When retrieving and processing email messages, you can obtain detailed identification information, such as sequence numbers and unique IDs, using the following features provided by the latest version of Aspose.Email for .NET:
Aspose.Email.ImapMessageInfo class: Represents the identification information about a message in an IMAP mailbox.
ImapMessageInfo.SequenceNumber property: Retrieves the sequence number of the message.
ImapMessageInfo.UniqueId property: Retrieves the unique identifier of the message.
Aspose.Email.MailMessage.ItemId property: Represents additional identification information about the message within the mailbox.
The following code snippet demonstrates how to obtain identification information for messages in an IMAP mailbox:
- Create an instance of the ImapClient class by providing the necessary parameters such as the IMAP server host, port, email address, password, and security options.
- Use the ListMessages method to retrieve a list of messages from the “INBOX” folder. Limit the list to the first five messages using the Take(5) method.
- Extract the sequence numbers of the listed messages by using the SequenceNumber property of each message.
- Use the FetchMessages method to retrieve the full details of the messages from the server, using the sequence numbers obtained in the previous step.
- Loop through the fetched messages and for each message, retrieve and display the following information:
- The sequence number of the message.
- The ItemId.SequenceNumber property.
- The subject of the message.
using (var client = new ImapClient(imapHost, port, emailAddress, password, securityOption))
{
// List the first 5 messages from the inbox
var msgs = client.ListMessages("INBOX").Take(5);
// Get sequence numbers of the messages
var seqIds = msgs.Select(t => t.SequenceNumber);
// Fetch messages based on sequence numbers
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}");
}
}
List 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.
List 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.
List Messages 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.
List 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.
List 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.
List 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.
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);
}
}
Fetching and Saving Messages
Fetch Messages from Server
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.
Fetch 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.
Save Messages in MSG 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 Fetched 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.
Fetch Folders and Read 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.
Handling Special Message Information
Retrieve Extra Parameters as Summary Information
Get 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.