Sample code for 30+ languages & platforms
Android™

Append (Upload) an Email to an IMAP Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMail method, which uploads an Email object to a mailbox on the server. The first argument is the destination mailbox name and the second is the Email. After a successful append, the AppendUid property holds the UID the server assigned (or 0 if none was reported). This example uploads a newly built message to the Drafts mailbox.

Background: The IMAP APPEND command lets a client add a message directly to any mailbox without sending it through SMTP — it is how "Save to Drafts" works, and how a client keeps its own copy in Sent. The initial \Seen state is governed by the AppendSeen property (or by the flag arguments of the AppendMimeWithFlags variants).

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.AppendMail method, which uploads an Email object to a mailbox on the
    //  server.  The 1st argument is the destination mailbox name and the 2nd is the Email.

    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.");

    //  Append (upload) the email to the "Drafts" mailbox.
    success = imap.AppendMail("Drafts",email);
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    //  AppendUid holds the UID assigned by the server (0 if the server did not report one).
    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."
  }
}