Working with Messages from Server
Getting Mailbox Information
- The GetMailBoxSize method returns the size of the mailbox in bytes.
- The GetMailBoxInfo method returns an object of type Pop3MailBoxInfo.
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:
- Create a Pop3Client.
- Connect to a POP3 server.
- Get the size of the mailbox.
- Get mailbox info.
- Get the number of messages in the mailbox.
- Get the occupied size.
Getting email count in the 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). This article shows how to retrieve and convert emails.
Retrieving Email Headers Information
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. That topic is specifically about sending an email with SMTP, but the email header content information remains valid for POP3 emails). The following examples show how to retrieve email headers from a POP3 server by the message’s sequence number.
Retrieving Email Messages
The Pop3Client class component provides the capability to retrieve email messages from the POP3 server, and parse them into a MailMessage instance with the help of MailMessage components. 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 retrieves a complete email message from the POP3 server.
Retrieving Message Summary Information using Unique Id
The POP3 Client of the API can retrieve message summary information from the server using the unique id of the message. This provides quick access to the message’s short information without first retrieving the complete message from the server. The following code snippet shows you how to retrieve message summary information.
Listing 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.
Fetching Messages from Server and saving 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 saves a message by its sequence number, in this case, number 1. The SaveMessage method saves the message in the original EML format without parsing it.
Parse Message Before Saving
Group Fetch 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.
Filtering Messages on Sender, Recipient or Date
The Pop3Client class, described in Connecting to a POP3 Server, provides the ListMessages() method which gets all the messages from a mailbox. To get only messages which match some condition, use the overloaded ListMessages() method which takes MailQuery as an argument. The MailQuery class provides various properties for specifying the query conditions, for example, date, subject, sender, recipient and so on. The MailQueryBuilder class is used to build the search expression. First, all the conditions and constraints are set and then MailQuery is filled with the query developed by MailQueryBuilder. The MailQuery class object is used by Pop3Client to extract the filtered information from the server. This article shows how to filter email messages from a mailbox. The first example illustrates how to filter messages based on date and subject. We also show how to filter on other criteria and how to build more complex queries. It also shows the application of Date and Time filter to retrieve the specific emails from the mailbox. In addition, it also shows how to apply case-sensitive filtering.
Filtering Messages from Mailbox
To filter messages from a mailbox:
- Connect and log in to a POP3 server.
- Create an instance of MailQuery and set the desired properties.
- Call the Pop3Client.ListMessages(MailQuery query) method and pass the MailQuery in parameters to get the filtered messages only.
The following code snippet shows you how to connect to a POP3 mailbox and get messages that arrived today and have the word “newsletter” in the subject.
Getting Messages that Meet Specific Criteria
The code samples above filter messages based on the email subject and date. We can use other properties to set other supported conditions as well. Below are some examples of setting the conditions using MailQuery.
The code snippets that follow show how to filter emails on other criteria:
- Find emails delivered today.
- Find emails received within a range.
- Find emails from a specific sender.
- Find emails sent from a specific domain.
- Find emails sent to a specific recipient.
The following code snippet shows you how to find emails delivered today.
The following code snippet shows you how to find emails received within a range.
The following code snippet shows you how to find emails from a specific sender.
The following code snippet shows you how to find emails sent from a specific domain.
The following code snippet shows you how to find emails sent to a specific recipient.
Building Complex Queries
If different MailQueryBuilder properties are set in separate statements, then all the conditions would be matched. For example, if we want to get messages between a date range and from a specific host, we need to write three statements.
Combining Queries with AND
The following code snippet shows you how to combine queries with AND.
Combining Queries with OR
MailQueryBuilder provides the Or() method which takes two MailQuery instances as parameters. It gets the messages that match any of the two conditions specified. The following code snippet shows how to filter messages that either have “test” in the subject or “email@example.com” as the sender. The following code snippet shows you how to combine queries with OR.
Applying Case Sensitive Filters
The API also provides the capability to filter emails from the mailbox based on a case sensitive criteria. The following methods provide the capability to search emails specifying case sensitive flag.
- Method Aspose.Email.StringComparisonField.Contains(string value, bool ignoreCase)
- Method Aspose.Email.StringComparisonField.Equals(string value, bool ignoreCase)
- Method Aspose.Email.StringComparisonField.NotContains(string value, bool ignoreCase)
- Method Aspose.Email.StringComparisonField.NotEquals(string value, bool ignoreCase)