Android™
Android™
Search an IMAP Mailbox
See more IMAP Examples
Demonstrates the Chilkat Imap.QueryMbx method, which searches the selected mailbox using IMAP search criteria and stores the matching message identifiers in a MessageSet. If bUid is true, the identifiers are UIDs; otherwise they are sequence numbers. This example finds unseen messages and lists their UIDs.
Background: IMAP search runs on the server, so you can find matching messages without downloading them. The criteria string is standard IMAP search syntax — for example
UNSEEN, FROM "alice@example.com", SUBJECT "invoice", or SINCE 1-Jan-2026 — and terms can be combined. The result is a MessageSet of identifiers you then fetch, flag, copy, or move. Requesting UIDs (bUid = true) is preferred because UIDs remain valid across sessions.Chilkat Android™ Downloads
// 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.QueryMbx method, which searches the selected mailbox using IMAP
// search criteria and stores the matching message identifiers in a MessageSet. If bUid is
// true, the identifiers are UIDs; otherwise they are sequence numbers.
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;
}
// Search for unseen (unread) messages, returning their UIDs.
CkMessageSet msgSet = new CkMessageSet();
success = imap.QueryMbx("UNSEEN",true,msgSet);
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
// Iterate the matching message identifiers.
int n = msgSet.get_Count();
Log.i(TAG, "Matching messages: " + String.valueOf(n));
int i;
for (i = 0; i <= n - 1; i++) {
Log.i(TAG, "UID: " + String.valueOf(msgSet.GetId(i)));
}
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."
}
}