Sample code for 30+ languages & platforms
Android™

Append a MIME Message to an IMAP Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMime method, which uploads a complete RFC 822/MIME message to a mailbox. The first argument is the destination mailbox name and the second is the MIME text. This example obtains the MIME from an Email object and uploads it to Drafts.

Background: Use AppendMime when you already hold a message's raw source — a .eml file, a message you assembled, or one you received. Chilkat sends the text exactly as supplied, without normalizing line endings or re-encoding headers, so every header and MIME boundary is preserved verbatim. The supplied text must be plain text (no raw non-text binary bytes).

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.AppendMime method, which uploads a complete MIME message to a mailbox.
    //  The 1st argument is the destination mailbox name and the 2nd is the MIME text.

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

    //  Obtain the MIME text to upload.  Here it is generated from the Email, but it could equally
    //  be read from a .eml file.
    String mimeText = email.getMime();
    if (email.get_LastMethodSuccess() == false) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    //  Append (upload) the MIME to the "Drafts" mailbox.
    success = imap.AppendMime("Drafts",mimeText);
    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."
  }
}