Sample code for 30+ languages & platforms
Android™

Read IMAP Email Headers

This example demonstrates how to connect to an IMAP server and download only the email headers for all messages in a mailbox. Downloading headers only is much faster and more efficient than downloading complete emails because the message bodies and attachment contents are not retrieved.

The example shows how to:

  • Connect to an IMAP server using SSL/TLS
  • Authenticate and select a mailbox
  • Query for all messages in the mailbox
  • Fetch header-only email information
  • Display sender, subject, recipients, and total message size
  • Retrieve attachment metadata such as filenames and sizes without downloading the actual attachments

This technique is useful for quickly scanning large mailboxes, building message summaries, or inspecting attachment information while minimizing bandwidth and memory usage.

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;

    success = false;

    // This example assumes the Chilkat API has already been unlocked.
    // See Global Unlock Sample for example code.

    CkImap imap = new CkImap();

    // Connect to the IMAP server using SSL/TLS on the standard secure IMAP port.
    imap.put_Ssl(true);
    imap.put_Port(993);
    success = imap.Connect("imap.example.com");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    // Authenticate with the IMAP server.
    success = imap.Login("****","****");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    // Select the mailbox (folder) to access.
    success = imap.SelectMailbox("Inbox");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    // Get the identifiers for all messages in the selected mailbox.
    // Setting fetchUids = cktrue causes IMAP UIDs to be returned.
    // If ckfalse, IMAP sequence numbers are returned instead.
    boolean fetchUids = true;
    CkMessageSet messageSet = new CkMessageSet();
    success = imap.QueryMbx("ALL",fetchUids,messageSet);
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    // Download only the email headers for the messages in the set.
    // This is much faster than downloading full emails because the
    // message bodies and attachment contents are not retrieved.
    // 
    // Even though the attachments themselves are not downloaded,
    // Chilkat can still provide information such as attachment
    // filenames, attachment sizes, and the total message size.
    CkEmailBundle bundle = new CkEmailBundle();
    boolean headersOnly = true;
    success = imap.FetchMsgSet(headersOnly,messageSet,bundle);
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    // Iterate over each email in the bundle and display summary information.
    CkEmail email = new CkEmail();
    String name;
    String addr;
    int i = 0;
    int j = 0;
    while (i < bundle.get_MessageCount()) {
        bundle.EmailAt(i,email);

        // Display the sender and subject line.
        Log.i(TAG, email.ck_from());
        Log.i(TAG, email.subject());

        // Display all "To" recipients.
        j = 0;
        while (j < email.get_NumTo()) {
            Log.i(TAG, "TO: " + email.getToName(j) + ", " + email.getToAddr(j));
            j = j + 1;
            }

        // Display all "CC" recipients.
        j = 0;
        while (j < email.get_NumCC()) {
            Log.i(TAG, "CC: " + email.getCcName(j) + ", " + email.getCcAddr(j));
            j = j + 1;
            }

        // Display the total size of the email message, including
        // the body and all attachments, even though only headers
        // were downloaded.
        Log.i(TAG, String.valueOf(email.get_Size()));

        // When full emails are downloaded, attachment information
        // is accessed using the email.NumAttachments property and
        // the email.GetMailAttach* methods.
        // 
        // However, because this example downloaded headers only,
        // attachment metadata must be obtained using the IMAP object.
        int numAttach = imap.GetMailNumAttach(email);

        j = 0;
        while (j < numAttach) {

            // Display the attachment filename.
            Log.i(TAG, imap.getMailAttachFilename(email,j));

            // Display the attachment size in bytes.
            int attachSize = imap.GetMailAttachSize(email,j);
            Log.i(TAG, "    size = " + String.valueOf(attachSize) + " bytes");

            j = j + 1;
            }

        Log.i(TAG, "--");

        i = i + 1;
        }

    // Disconnect from the IMAP server.
    success = imap.Disconnect();

  }

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