Sample code for 30+ languages & platforms
Android™

Add an Attachment from a BinData Object

See more Email Object Examples

Demonstrates the Chilkat Email.AddAttachmentBd method, which adds an attachment using the contents of a BinData object. The first argument is the attachment filename, the second is the BinData, and the third is the content type — if empty, it is inferred from the filename extension. This example loads a PDF into a BinData and attaches it.

Background: BinData is Chilkat's container for raw binary data. Attaching from a BinData is the right approach when the file's bytes are already in memory — generated on the fly, downloaded, or read from a database — rather than sitting on disk (which would use AddFileAttachment). Chilkat Base64-encodes the bytes into the message automatically.

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 AddAttachmentBd method, which adds an attachment using the contents of a
    //  BinData object.  The first argument is the attachment filename, the second is the BinData
    //  object, and the third is the content type (inferred from the filename extension if empty).

    CkEmail email = new CkEmail();
    email.put_Subject("Attach from BinData");
    email.put_Body("Please see the attached file.");

    //  Load a file into a BinData object, then attach it.
    CkBinData bd = new CkBinData();
    success = bd.LoadFile("qa_data/attachments/report.pdf");
    if (success == false) {
        Log.i(TAG, bd.lastErrorText());
        return true;
        }

    success = email.AddAttachmentBd("report.pdf",bd,"application/pdf");
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    Log.i(TAG, "NumAttachments = " + String.valueOf(email.get_NumAttachments()));

    //  Note: The path "qa_data/attachments/report.pdf" 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."
  }
}