Sample code for 30+ languages & platforms
Android™

Append MIME with Initial IMAP Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMimeWithFlags method, which uploads a MIME message and sets its initial system flags. The first argument is the mailbox and the second is the MIME text. The third through sixth arguments control \Seen, \Flagged, \Answered, and \Draft respectively — pass true to set a flag. This example uploads a message that is already marked read.

Background: When importing or archiving mail you frequently want the uploaded message to arrive in a particular state — already read, pre-flagged, or marked as a draft. Setting the flags at append time is a single operation, avoiding a second STORE round trip. These explicit arguments take precedence over the AppendSeen property.

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.AppendMimeWithFlags method, which uploads a MIME message and sets its
    //  initial system flags.  The 1st argument is the mailbox, the 2nd is the MIME text, and the
    //  3rd through 6th arguments set the \Seen, \Flagged, \Answered, and \Draft flags.

    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;
        }

    //  Build the email to be uploaded.
    CkEmail email = new CkEmail();
    email.put_Subject("Meeting agenda");
    email.put_From("Alice <alice@example.com>");
    success = email.AddTo("Bob","bob@example.com");
    email.put_Body("Let's meet at 10am to review the agenda.");

    String mimeText = email.getMime();
    if (email.get_LastMethodSuccess() == false) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    //  Choose the initial flags.  Named variables make each argument's meaning clear at the call.
    boolean seen = true;
    boolean flagged = false;
    boolean answered = false;
    boolean draft = false;

    //  Upload to the Inbox already marked as read (\Seen), with the other flags unset.
    success = imap.AppendMimeWithFlags("Inbox",mimeText,seen,flagged,answered,draft);
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    Log.i(TAG, "Appended UID: " + String.valueOf(imap.get_AppendUid()));

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