Sample code for 30+ languages & platforms
Android™

Attach an Email to Another Email

See more Email Object Examples

Demonstrates the Chilkat Email.AttachEmail method, which attaches a copy of another email to this email object. The attached email is encapsulated in a message/rfc822 subpart. Because a copy is attached, later changes to the source email do not affect the embedded message. This example attaches one email to another.

Background: "Forward as attachment" produces exactly this structure: the original message is embedded whole as a message/rfc822 part rather than quoted into the body. This preserves the original's headers and formatting intact, which matters for forwarding to a mailbox that will re-parse it, or for reporting spam/phishing with the original evidence attached. Such parts are counted by NumAttachedMessages.

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 AttachEmail method, which attaches a copy of another email to this email
    //  object.  The attached email is encapsulated in a message/rfc822 subpart.

    CkEmail innerEmail = new CkEmail();
    innerEmail.put_Subject("Original message");
    innerEmail.put_From("alice@example.com");
    innerEmail.put_Body("This is the original message being forwarded.");

    CkEmail email = new CkEmail();
    email.put_Subject("FW: Original message");
    email.put_From("bob@example.com");
    email.put_Body("See the attached original email.");

    //  Attach a copy of the inner email as a message/rfc822 part.

    success = email.AttachEmail(innerEmail);
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    Log.i(TAG, "NumAttachedMessages = " + String.valueOf(email.get_NumAttachedMessages()));

  }

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