Sample code for 30+ languages & platforms
Android™

Strip Path Info from Attachment Filenames

See more Email Object Examples

Demonstrates the Chilkat Email.RemoveAttachmentPaths method, which removes relative or absolute path information from the attachment filenames stored in the MIME, leaving only each final filename component. This example adds an attachment whose name includes a path and shows the filename before and after.

Background: An attachment filename that carries a path (like reports/2026/q3/summary.txt) is a problem: some mail clients display the whole path, and — more seriously — a path with .. segments can be a directory-traversal risk when attachments are saved to disk. Reducing every filename to just its final component (summary.txt) makes the message cleaner and safer to handle.

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 RemoveAttachmentPaths method, which removes relative or absolute path
    //  information from attachment filenames stored in the MIME, leaving only each final
    //  filename component.

    CkEmail email = new CkEmail();
    email.put_Subject("Remove attachment paths");

    //  Add an attachment whose filename includes path information.
    email.AddStringAttachment("reports/2026/q3/summary.txt","Attachment content.");
    Log.i(TAG, "Filename before: " + email.getAttachmentFilename(0));

    //  Strip the path, leaving only the final filename component.
    email.RemoveAttachmentPaths();
    Log.i(TAG, "Filename after: " + email.getAttachmentFilename(0));

  }

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