Sample code for 30+ languages & platforms
Android™

Get Email HTML Body with Images as Base64 Inline Data

See more Email Object Examples

Demonstrates how to get the email's HTML body with referenced MIME parts (i.e. related images) embedded in the HTML as base64 data.

Note: This example requires Chilkat v10.1.2 or later.

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;

    // ---------------------------------------------------------------------------------------------------------------------------------
    // Prerequisite Knowledge:

    // In HTML emails, CID (Content-ID) image references allow embedding images directly into the email as part of the message body, 
    // instead of linking to external images. 

    // * Each embedded image is assigned a unique Content-ID (CID) in the email�s MIME structure.
    // * The CID is a unique identifier, such as image001@domain.com.

    // For example: <img src="cid:image001@domain.com" alt="Embedded Image">
    // ---------------------------------------------------------------------------------------------------------------------------------

    // However, in HTML, you can embed image data directly into an <img> tag by using Base64 encoding. 
    // This method eliminates the need for external image files by encoding the image as a string and including it within the HTML.

    // For example:
    // <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA .... RU5ErkJggg==" alt="Embedded Image">

    // Chilkat added the GetHtmlBodySb method in version 10.1.2 which provides an option for gathering the 
    // image data from the mulitpart/related MIME parts of the email, and replaces the "cid" img references with
    // the base64 data.

    CkEmail email = new CkEmail();
    success = email.LoadEml("qa_data/eml/sample.eml");
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    CkStringBuilder sbHtml = new CkStringBuilder();
    boolean inlineImageData = true;
    success = email.GetHtmlBodySb(inlineImageData,sbHtml);
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    // The HTML with inlined base64 image data is now contained in the StringBuilder.
    String htmlStr = sbHtml.getAsString();

    sbHtml.WriteFile("c:/temp/qa_output/sample.html","utf-8",false);

    Log.i(TAG, "Success.");

  }

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