Android™
Android™
Save All MIME Attachments to Files
See more MIME Examples
Demonstrates the Chilkat Mime.PartsToFiles method, which recursively traverses the MIME tree and saves each part having a nonempty filename parameter to a directory. The first argument is the destination directory and the second is a StringTable that receives the saved filenames.
Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background: This extracts every named attachment from a message in one call, walking the whole tree so it finds attachments nested inside multipart containers. It saves only parts that declare a
filename — inline body parts without one are skipped — and reports what it wrote via the StringTable, which is handy for logging or follow-up processing. The destination directory must already exist.Chilkat Android™ Downloads
// 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;
CkMime mime = new CkMime();
success = mime.LoadMimeFile("qa_data/multipart_message.eml");
if (success == false) {
Log.i(TAG, mime.lastErrorText());
return;
}
// Recursively traverse the MIME tree and save each part that has a nonempty "filename" parameter
// to a directory. The 1st argument is the destination directory and the 2nd is a StringTable
// that receives the saved filenames.
CkStringTable savedFiles = new CkStringTable();
success = mime.PartsToFiles("qa_output/attachments",savedFiles);
if (success == false) {
Log.i(TAG, mime.lastErrorText());
return;
}
int n = savedFiles.get_Count();
Log.i(TAG, "Saved " + String.valueOf(n) + " files:");
int i;
for (i = 0; i <= n - 1; i++) {
String name = savedFiles.stringAt(i);
if (savedFiles.get_LastMethodSuccess() == false) {
Log.i(TAG, savedFiles.lastErrorText());
return;
}
Log.i(TAG, name);
}
}
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."
}
}