Sample code for 30+ languages & platforms
Android™ Requires Chilkat v11.0.0+

Add an attachment and an inline image

See more Email Object Examples

Add a file attachment from memory and embed an image in the HTML body using its Content-ID. Start with any email that has an HTML body, or use text-and-html.eml from the preceding example. Save the result as with-attachments.eml.

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;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    CkEmail email = new CkEmail();
    success = email.LoadEml("text-and-html.eml");
    if (success != true) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    CkBinData data = new CkBinData();
    success = data.AppendString("Time,Activity\r\n09:00,Welcome\r\n10:00,Field session\r\n","utf-8");
    if (success != true) {
        Log.i(TAG, "Failed: data.AppendString.");
        return;
        }

    success = email.AddAttachmentBd("agenda.csv",data,"text/csv");
    if (success != true) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    //  AddRelatedBd returns the identifier that must appear after cid: in the HTML.
    CkBinData image = new CkBinData();
    success = image.AppendEncoded("iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAASUlEQVR4nGMQK/ImBv3//x/CYCBSNQkaIKqJ1YCmGkUDsigu1egakOWwqsaiAaICl2p0P8DV4VKNxdP4VWMPJTyqiY04sZGuAQBhyhp6A/ap2AAAAABJRU5ErkJggg==","base64");
    if (success != true) {
        Log.i(TAG, "Failed: image.AppendEncoded.");
        return;
        }

    String cid = email.addRelatedBd("confirmed.png",image);
    if (email.get_LastMethodSuccess() != true) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    CkStringBuilder html = new CkStringBuilder();
    html.Append("<p><img alt='Confirmed' src=\"cid:");
    html.Append(cid);
    html.Append("\"></p>");
    html.Append(email.getHtmlBody());
    success = email.AddHtmlAlternativeBody(html.getAsString());
    if (success != true) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    success = email.SaveEml("with-attachments.eml");
    if (success != true) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    Log.i(TAG, "Saved with-attachments.eml.");

  }

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