Sample code for 30+ languages & platforms
Android™

Fetch a Single IMAP Message into an Email Object

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchEmail method, which downloads one message (or its headers) into an Email object. The arguments are headerOnly, the message id, bUid (UID vs sequence number), and the Email that receives the message. This example fetches message 1 in full and reads its subject and from.

Background: FetchEmail parses a downloaded message into a fully navigable Email object, giving you its subject, sender, body, and attachments. Passing headerOnly = true downloads just the headers — a fast way to build a message-list preview — after which you can fetch the full message on demand. Because a mailbox must be selected first, and fetching a full message can set the \Seen flag, use ExamineMailbox instead of SelectMailbox when you must not change read state.

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.FetchEmail method, which downloads one message (or its headers) into
    //  an Email object.  The 1st argument is headerOnly, the 2nd is the message id, the 3rd
    //  (bUid) selects UID vs sequence number, and the 4th is the Email that receives the message.

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

    //  Download message sequence number 1 in full.  headerOnly=false, bUid=false.
    CkEmail email = new CkEmail();
    success = imap.FetchEmail(false,1,false,email);
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    Log.i(TAG, "Subject: " + email.subject());
    Log.i(TAG, "From: " + email.ck_from());

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