Sample code for 30+ languages & platforms
Android™

Fetch a Range of IMAP Messages by Sequence Number

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchRange method, which downloads a contiguous run of messages by sequence number. The arguments are headersOnly, the starting sequence number, the count of messages, and the EmailBundle that receives them. This example uses the message count returned by SelectMailbox to fetch the headers of the 10 most recent messages.

Background: IMAP sequence numbers are 1-based positions within the selected mailbox, from 1 (oldest) to the message count (newest), and they shift whenever messages are expunged. That makes FetchRange ideal for "give me the newest N" style paging within a single session, but note that sequence numbers are not stable identifiers across sessions the way UIDs are — use a UID-based approach if you need to remember which messages you have already seen.

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.FetchRange method, which downloads a contiguous run of messages by
    //  sequence number.  The 1st argument is headersOnly, the 2nd is the starting sequence number,
    //  the 3rd is the count of messages, and the 4th is the EmailBundle that receives them.

    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;
        }

    //  SelectMailbox returns the number of messages in the mailbox, or -1 on failure.
    int numMessages = imap.SelectMailbox("Inbox");
    if (numMessages < 0) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    //  Download the 10 most recent messages (headers only).
    int startSeqnum = numMessages - 9;
    if (startSeqnum < 1) {
        startSeqnum = 1;
        }

    CkEmailBundle bundle = new CkEmailBundle();
    success = imap.FetchRange(true,startSeqnum,10,bundle);
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    int n = bundle.get_MessageCount();
    Log.i(TAG, "Fetched " + String.valueOf(n) + " message headers.");

    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."
  }
}