Android™
Android™
Move IMAP Messages to Another Mailbox
See more IMAP Examples
Demonstrates the Chilkat Imap.MoveMessages method, which moves the messages in a MessageSet to another mailbox on the server. The first argument is the MessageSet and the second is the destination folder name. This example searches for newsletter messages and moves them to the Archive mailbox.
Background: A true server-side move (the IMAP
MOVE extension) is atomic and efficient: the message never leaves the server, so no download/re-upload is involved. If the server lacks the MOVE capability, the equivalent is copy-then-delete-then-expunge — Chilkat handles that fallback for you, but the destination mailbox must already exist.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.MoveMessages method, which moves the messages in a MessageSet to
// another mailbox on the server. The 1st argument is the MessageSet and the 2nd is the
// destination folder name.
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;
}
// Find the messages to move (here, messages from a particular sender, by UID).
CkMessageSet msgSet = new CkMessageSet();
success = imap.QueryMbx("FROM \"newsletter@example.com\"",true,msgSet);
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
// Move them to the "Archive" mailbox.
success = imap.MoveMessages(msgSet,"Archive");
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
Log.i(TAG, "Moved " + String.valueOf(msgSet.get_Count()) + " messages to Archive.");
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."
}
}