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

Find and save an attachment and list inline images

See more Email Object Examples

Find an attachment by filename and media type, then save its decoded bytes to a path you choose. Enumerate inline resources separately to read their Content-IDs.

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("with-attachments.eml");
    if (success != true) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    CkStringBuilder filename = new CkStringBuilder();
    CkStringBuilder mediaType = new CkStringBuilder();
    boolean found = false;
    int i = 0;
    while ((i < email.get_NumAttachments())) {
        filename.SetString(email.getAttachmentFilename(i));
        mediaType.SetString(email.getAttachmentContentType(i));
        if ((filename.ContentsEqual("agenda.csv",false) == true) and (mediaType.ContentsEqual("text/csv",false) == true) and (found == false)) {
            CkBinData data = new CkBinData();
            success = email.GetAttachmentBd(i,data);
            if (success != true) {
                Log.i(TAG, email.lastErrorText());
                return;
                }

            success = data.WriteFile("received-agenda.csv");
            if (success != true) {
                Log.i(TAG, "Failed: data.WriteFile.");
                return;
                }

            Log.i(TAG, "Saved attachment: " + String.valueOf(data.get_NumBytes()) + " bytes.");
            found = true;
            }

        i = i + 1;
        }

    if (found != true) {
        Log.i(TAG, "No matching attachment.");
        }

    i = 0;
    while ((i < email.get_NumRelatedItems())) {
        Log.i(TAG, "Related: " + email.getRelatedContentType(i) + "  Content-ID: " + email.getRelatedContentID(i));
        i = i + 1;
        }


  }

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