Working with ImapClient Asynchronously

Messages can be retrieved from mailbox asynchronously by using the Aspose.Email ImapClient. This article shows retrieving messages from mailbox Asynchronously. This article also shows how to list messages by providing search criteria using MailQuery.

Retrieve Messages Asynchronously

The following code snippet shows you how to retrieve messages asynchronously.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Connect and log in to IMAP
try (ImapClient client = new ImapClient("host", "username", "password")) {
    client.selectFolder("Issues/SubFolder");
    ImapMessageInfoCollection messages = client.listMessages();
    IAsyncResult ar = client.beginFetchMessage(messages.get_Item(0).getSequenceNumber());
    MailMessage message = client.endFetchMessage(ar);
}

List Messages Asynchronously with MailQuery

The MailQuery class can be used to specify search criteria for retrieving a specified list of messages asynchronously as is shown in the following code sample.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
ImapQueryBuilder builder = new ImapQueryBuilder();
builder.getSubject().contains("Subject");
MailQuery query = builder.getQuery();
IAsyncResult asyncResult = client.beginListMessages(query);
ImapMessageInfoCollection messages = client.endListMessages(asyncResult);

Append Messages Asynchronously

Uploading messages asynchronously is convenient, as the process does not block the execution of the program or thread. Instead of waiting for the whole batch to be appended before proceeding with other tasks, the program can continue running while the messages are uploaded in the background.

Aspose.Email for Java implements asynchronous operations with the classic beginXxx / endXxx pair, which returns an IAsyncResult:

List<MailMessage> mailMessages = new ArrayList<MailMessage>();

// Create the mail messages
for (int i = 0; i < 100; i++) {
    mailMessages.add(new MailMessage("sender@domain.com", "receiver@domain.com", "Message #" + i, "Text"));
}

try (ImapClient client = new ImapClient("host", 993, "sender@domain.com", "password", SecurityOptions.SSLImplicit)) {
    client.selectFolder(ImapFolderInfo.IN_BOX);

    // Start uploading the messages
    IAsyncResult asyncResult = client.beginAppendMessages(mailMessages);

    // ... do other work here ...

    // Wait for the operation to complete
    AppendMessagesResult result = client.endAppendMessages(asyncResult);
    System.out.println("All messages have been appended: " + result);
}

Cancel Asynchronous Operations

Sometimes you may need to stop an asynchronous operation before it completes. The cancelAsyncOperation method, inherited from EmailClient, interrupts an operation started with any of the beginXxx methods — pass it the IAsyncResult returned when the operation was started:

List<MailMessage> mailMessages = new ArrayList<MailMessage>();
for (int i = 0; i < 100; i++) {
    mailMessages.add(new MailMessage("sender@domain.com", "receiver@domain.com", "Message #" + i, "Text"));
}

try (ImapClient client = new ImapClient("host", 993, "sender@domain.com", "password", SecurityOptions.SSLImplicit)) {
    client.selectFolder(ImapFolderInfo.IN_BOX);

    IAsyncResult asyncResult = client.beginAppendMessages(mailMessages);

    // Let the upload run for a while, then interrupt it
    Thread.sleep(5000);
    client.cancelAsyncOperation(asyncResult);

    System.out.println("Operation has been interrupted.");
}