Sample code for 30+ languages & platforms
Android™

Fetch a MessageSet into an EmailBundle

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchMsgSet method, which downloads the messages identified by a MessageSet and appends them to an EmailBundle. The arguments are headersOnly, the MessageSet, and the EmailBundle (which is not cleared). This example searches for unseen messages and downloads the whole set.

Background: This is the natural pairing with QueryMbx: search to get a MessageSet of matching identifiers, then download exactly those messages in one call. Fetching a set is far more efficient than looping one message at a time, and it returns an EmailBundle you iterate with MessageCount and EmailAt. Pass headersOnly = true to pull just headers for a fast preview list.

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.FetchMsgSet method, which downloads the messages identified by a
    //  MessageSet and appends them to an EmailBundle.  The 1st argument is headersOnly, the 2nd
    //  is the MessageSet, and the 3rd is the EmailBundle (which is not cleared).

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

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

    //  Find the messages to download (here, the unseen messages by UID).
    CkMessageSet msgSet = new CkMessageSet();
    success = imap.QueryMbx("UNSEEN",true,msgSet);
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    //  Download the messages in the set (full bodies) into a bundle.
    CkEmailBundle bundle = new CkEmailBundle();
    success = imap.FetchMsgSet(false,msgSet,bundle);
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    int n = bundle.get_MessageCount();
    Log.i(TAG, "Fetched " + String.valueOf(n) + " messages.");
    CkEmail email = new CkEmail();
    int i;
    for (i = 0; i <= n - 1; i++) {
        success = bundle.EmailAt(i,email);
        Log.i(TAG, email.subject());
        }

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