Sample code for 30+ languages & platforms
Android™

Copy Multiple IMAP Messages to Another Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.CopyMultiple method, which copies the messages in a MessageSet to another mailbox, leaving the originals in place. The first argument is the MessageSet and the second is the destination mailbox name. This example searches for recent messages and copies them to a Backup mailbox.

Background: This maps to the IMAP COPY command, which duplicates messages entirely on the server — nothing is downloaded. The copy in the destination mailbox is an independent message with its own UID; changing flags on one copy does not affect the other. Combine CopyMultiple with a search from QueryMbx to snapshot or archive matching messages without removing them from their current folder.

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.CopyMultiple method, which copies the messages in a MessageSet to
    //  another mailbox, leaving the originals in place.  The 1st argument is the MessageSet and
    //  the 2nd is the destination mailbox 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 copy (here, messages seen today, by UID).
    CkMessageSet msgSet = new CkMessageSet();
    success = imap.QueryMbx("SINCE 15-Jul-2026",true,msgSet);
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    //  Copy them to the "Backup" mailbox.  The originals remain in the Inbox.
    success = imap.CopyMultiple(msgSet,"Backup");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    Log.i(TAG, "Copied " + String.valueOf(msgSet.get_Count()) + " messages to Backup.");

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