Sample code for 30+ languages & platforms
Android™

Get the Filename of an Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachmentFilename method, which retrieves an attachment's filename as stored in the MIME. The index is zero-based. This example adds an attachment and reads its filename.

Background: An attachment's filename comes from the sender's message and is not guaranteed to be safe or unique — it may include path separators or characters that are awkward on the local filesystem, and two attachments can share a name. When saving attachments to disk, treat this value as untrusted input: sanitize it and guard against collisions (see the OverwriteExisting property, which can auto-generate unique names).

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);

    //  Demonstrates the GetAttachmentFilename method, which retrieves an attachment's filename
    //  (as stored in the MIME).  The index is zero-based.

    CkEmail email = new CkEmail();
    email.put_Subject("Attachment filename");

    email.AddStringAttachment("notes.txt","Some notes.");

    //  Get the filename of the first attachment (index 0).
    String fname = email.getAttachmentFilename(0);
    Log.i(TAG, "Attachment 0 filename: " + fname);

  }

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