Android™ Requires Chilkat v11.0.0+
Android™
Find an email by subject and fetch it by IMAP UID
See more Email Object Examples
Search the selected INBOX by subject and fetch the first matching UID into an Email. The example requests complete attachment bodies and uses peek mode to preserve the read flag. Configure your own IMAP TLS endpoint and credentials.
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;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkImap imap = new CkImap();
imap.put_Ssl(true);
imap.put_Port(993);
imap.put_RequireSslCertVerify(true);
imap.put_PeekMode(true);
imap.put_AutoDownloadAttachments(true);
success = imap.Connect("imap.example.com");
if (success != true) {
Log.i(TAG, imap.lastErrorText());
return;
}
success = imap.Login("MAIL_USER","MAIL_PASSWORD");
if (success != true) {
Log.i(TAG, imap.lastErrorText());
return;
}
success = imap.SelectMailbox("INBOX");
if (success != true) {
Log.i(TAG, imap.lastErrorText());
return;
}
CkMessageSet matches = new CkMessageSet();
success = imap.QueryMbx("SUBJECT \"Sample message\"",true,matches);
if (success != true) {
Log.i(TAG, imap.lastErrorText());
return;
}
if (matches.get_Count() == 0) {
Log.i(TAG, "No matching message in INBOX.");
success = imap.Disconnect();
if (success != true) {
Log.i(TAG, imap.lastErrorText());
return;
}
return;
}
// The search returned UIDs. Use the same identifier type for the fetch.
int uid = matches.GetId(0);
CkEmail email = new CkEmail();
success = imap.FetchEmail(false,uid,true,email);
if (success != true) {
Log.i(TAG, imap.lastErrorText());
return;
}
Log.i(TAG, "Mailbox INBOX, UIDVALIDITY " + String.valueOf(imap.get_UidValidity()) + ", UID " + String.valueOf(uid));
success = imap.Disconnect();
if (success != true) {
Log.i(TAG, imap.lastErrorText());
return;
}
// The downloaded Email remains usable after disconnecting.
Log.i(TAG, "Subject: " + email.subject() + " Files: " + String.valueOf(email.get_NumAttachments()));
success = email.SaveEml("imap-received.eml");
if (success != true) {
Log.i(TAG, email.lastErrorText());
return;
}
Log.i(TAG, "Saved imap-received.eml.");
}
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."
}
}