Sample code for 30+ languages & platforms
Android™

Change the Filename of an Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.SetAttachmentFilename method, which changes the filename of the attachment at a given zero-based index. This example adds an attachment and renames it, printing the filename before and after.

Background: The attachment filename is what a recipient sees and what most clients suggest when saving the file. Renaming it is handy when the original name is unclear, unsafe, or generic — for example giving a machine-generated tmp12345 a meaningful name like invoice.pdf before sending.

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 SetAttachmentFilename method, which changes the filename of the
    //  attachment at the given zero-based index.

    CkEmail email = new CkEmail();
    email.put_Subject("Set attachment filename");

    email.AddStringAttachment("oldname.txt","Some notes.");
    Log.i(TAG, "Filename before: " + email.getAttachmentFilename(0));

    //  Change the filename of the first attachment (index 0).
    success = email.SetAttachmentFilename(0,"newname.txt");
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

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