Retrieve and Manage Messages from Mail Server

Get Mailbox Information

We can get information about the mailbox such as the number of messages and the mailbox size using the GetMailBoxSize and GetMailBoxInfo methods of the Pop3Client class.

It is also possible to get the number of messages using the MessageCount property and the size using the OccupiedSize property of the Pop3MailBoxInfo class. The following sample code shows how to get information about the mailbox. It shows how to:

  1. Create a Pop3Client.
  2. Connect to a POP3 server.
  3. Get the size of the mailbox.
  4. Get mailbox info.
  5. Get the number of messages in the mailbox.
  6. Get the occupied size.

Retrieve Email Count in Mailbox

The following code snippet shows you how to count the email messages in a mailbox.

Aspose.Email lets developers work with emails in many different ways. For example, they can retrieve header information before deciding whether to download an email. Or they can retrieve emails from a server and save them without parsing them (quicker) or after parsing them (slower).

Retrieve Email Headers

Email headers can give us information about an email message that we can use to decide whether or not to retrieve the whole email message. Typically, the header information contains sender, subject, received date, etc. (Email headers are described in detail in Customizing Email Headers. The following examples show how to retrieve email headers from a POP3 server by the message sequence number.

Retrieve Email Messages

The MailMessage class contains several properties and methods for manipulating email content. By using FetchMessage method of the Pop3Client class, you can get a MailMessage instance directly from the POP3 server. The following code snippet shows you how to retrieve a complete email message from the POP3 server.

Retrieve Message Summary with Unique Id

The POP3 Client can retrieve message summary information from the server using the unique id of the message. This provides quick access to the message short information without first retrieving the complete message from the server. The following code snippet shows you how to retrieve message summary information.

List Messages with MultiConnection

Pop3Client 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 Pop3Client.ConnectionsQuantity. The following code snippet demonstrates the use of the multiconnection mode for listing messages and compares its performance with single connection mode.

Fetch Messages from Server and Save to Disc

Save Message to Disk without Parsing

If you want to download email messages from the POP3 server without parsing them, use the Pop3Client class SaveMessage function. The SaveMessage function does not parse the email message so it is faster than the FetchMessage function. The following code snippet shows how to save a message by its sequence number. In this case, the SaveMessage method saves the message in the original EML format without parsing it.

Parse Message Before Saving

The following code snippet uses the Pop3Client FetchMessage method to retrieve a message from a POP3 server by its sequence number, then save the message to disk using the subject as the file name.

Fetch Group Messages

Pop3Client 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.