Sample code for 30+ languages & platforms
Android™

Add a Related File Addressed by Content-Location

See more Email Object Examples

Demonstrates the Chilkat Email.AddRelatedFile2 method, which adds a local file as a related MIME resource addressed by Content-Location rather than Content-ID. The second argument becomes the related item's MIME filename and Content-Location value, and it should match the filename, path, or URL already used by the corresponding HTML reference (such as an img element's src). This example embeds an image referenced as logo.png.

Background: There are two ways HTML email links to embedded resources. The cid: approach (see AddRelatedFile) uses a generated Content-ID, while the Content-Location approach lets the HTML keep an ordinary-looking src="logo.png" and matches it to a part carrying the same Content-Location. The latter is handy when converting an existing web page to email, because the original relative URLs can stay unchanged.

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 AddRelatedFile2 method, which adds a local file as a related MIME
    //  resource addressed by Content-Location rather than Content-ID.  The 2nd argument becomes
    //  the item's MIME filename / Content-Location and should match the reference in the HTML.

    CkEmail email = new CkEmail();
    email.put_Subject("Email with a related image (by 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>");

    //  Add the file as a related item addressed by Content-Location "logo.png".
    success = email.AddRelatedFile2("qa_data/images/logo.png","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."
  }
}