Android™
Android™
Set a Single Flag on One IMAP Message
See more IMAP Examples
Demonstrates the Chilkat Imap.SetFlag method, which sets or clears a single flag on one message identified by ID. The first argument is the message ID, the second (bUid) selects UID vs sequence number, the third is the flag name, and the fourth is the value (1 to set, 0 to clear). This example flags one message as Deleted and then calls Expunge to permanently remove it.
Background: Deleting an IMAP message is a two-step process by design: setting the
Deleted flag only marks the message, and Expunge is what actually removes every message so marked. This separation lets a client show "deleted" items and offer an undo before the change becomes permanent. SetFlag handles one flag on one message; use StoreFlags to change several flags at once, or SetFlags to update a whole MessageSet.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.SetFlag method, which sets or clears a single flag on one message
// identified by ID. The 1st argument is the message ID, the 2nd (bUid) selects UID vs
// sequence number, the 3rd is the flag name, and the 4th is the value (1 to set, 0 to clear).
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;
}
CkMessageSet msgSet = new CkMessageSet();
success = imap.QueryMbx("ALL",true,msgSet);
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
if (msgSet.get_Count() > 0) {
// Mark the first message for deletion. The "Deleted" flag is later acted upon by Expunge.
int uid = msgSet.GetId(0);
success = imap.SetFlag(uid,true,"Deleted",1);
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
// Permanently remove all messages flagged as Deleted.
success = imap.Expunge();
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
Log.i(TAG, "Deleted UID " + String.valueOf(uid));
}
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."
}
}