Sample code for 30+ languages & platforms
Android™

Add a Related Item from BinData by Content-Location

See more Email Object Examples

Demonstrates the Chilkat Email.AddRelatedBd2 method, which adds a related item using the binary data in a BinData object, addressed by Content-Location. The second argument specifies the filename, path, or URL used by the corresponding HTML reference. This example embeds an image referenced as logo.png.

Background: This is the binary counterpart of AddRelatedString2 and the Content-Location sibling of AddRelatedBd. Instead of a generated cid: reference, the HTML keeps an ordinary src="logo.png" and the related part is matched to it by name — convenient when turning an existing web page into an email so its original relative URLs keep working.

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 AddRelatedBd2 method, which adds a related item using the binary data in
    //  a BinData object, addressed by Content-Location.  The second argument specifies the filename/path/URL
    //  used by the corresponding HTML reference.

    CkEmail email = new CkEmail();
    email.put_Subject("Related image from BinData (Content-Location)");

    //  The HTML references the image by the same name used as the Content-Location.
    email.SetHtmlBody("<html><body><img src=\"logo.png\"/></body></html>");

    //  Load the image data from a file into a BinData object.
    CkBinData bdImage = new CkBinData();
    success = bdImage.LoadFile("qa_data/images/logo.png");
    if (success == false) {
        Log.i(TAG, bdImage.lastErrorText());
        return true;
        }

    //  Add the image as a related item addressed by Content-Location "logo.png".
    success = email.AddRelatedBd2(bdImage,"logo.png");
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    Log.i(TAG, "NumRelatedItems = " + String.valueOf(email.get_NumRelatedItems()));

    //  Note: The path "qa_data/images/logo.png" is a relative local filesystem path,
    //  relative to the current working directory of the running application.

  }

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