Sample code for 30+ languages & platforms
Android™

Fetch a Chunk of Messages, Tracking Failures

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchChunk2 method, which downloads a run of messages by sequence number and reports which succeeded and which failed. The arguments are the starting sequence number, the count, a MessageSet that receives the failed IDs, a MessageSet that receives the successfully-fetched IDs, and the EmailBundle. This example fetches the first chunk of messages and reports the success and failure counts.

Background: When bulk-downloading a large mailbox, individual messages can occasionally fail to fetch (a corrupt message on the server, a transient error) without failing the whole operation. FetchChunk2 is designed for that reality: it returns a partial EmailBundle plus two MessageSet objects so your code can log or retry exactly the messages that did not come through, rather than aborting the entire sync.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  Demonstrates the Imap.FetchChunk2 method, which downloads a run of messages by sequence
    //  number and reports which succeeded and which failed.  The 1st argument is the starting
    //  sequence number, the 2nd is the count, the 3rd receives the failed IDs, the 4th receives
    //  the successfully-fetched IDs, and the 5th is the EmailBundle.

    CkImap imap = new CkImap();

    imap.put_Ssl(true);
    imap.put_Port(993);

    success = imap.Connect("imap.example.com");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    success = imap.Login("user@example.com","myPassword");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    int numMessages = imap.SelectMailbox("Inbox");
    if (numMessages < 0) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    //  Fetch messages 1..25 (or fewer if the mailbox is smaller).
    int count = 25;
    if (count > numMessages) {
        count = numMessages;
        }

    CkMessageSet failedSet = new CkMessageSet();
    CkMessageSet fetchedSet = new CkMessageSet();
    CkEmailBundle bundle = new CkEmailBundle();
    success = imap.FetchChunk2(1,count,failedSet,fetchedSet,bundle);
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    Log.i(TAG, "Fetched: " + String.valueOf(fetchedSet.get_Count()));
    Log.i(TAG, "Failed:  " + String.valueOf(failedSet.get_Count()));

    success = imap.Disconnect();
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}