Android™
Android™
Attach a File to an Email
See more Email Object Examples
Demonstrates the Chilkat Email.AddFileAttachment method, which attaches a file read from the filesystem. It returns the content type Chilkat assigned to the attachment (inferred from the file extension). This example attaches a PDF and prints its detected content type.
Background: Each attachment carries a
Content-Type (MIME type) such as application/pdf or image/png that tells the receiving client how to handle it. Chilkat gets this from the file's extension. Because attachment bytes are binary, they are Base64-encoded for transport, which is handled automatically — you simply point AddFileAttachment at a path and the file is read, encoded, and packaged into the message.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);
// Demonstrates the AddFileAttachment method, which attaches a file read from the
// filesystem. It returns the content type Chilkat assigned to the attachment (based on
// the file extension), or returns failure if the file could not be read.
CkEmail email = new CkEmail();
email.put_Subject("Email with a file attachment");
email.put_Body("Please see the attached file.");
// Attach a file. The return value is the auto-detected content type.
String contentType = email.addFileAttachment("qa_data/attachments/report.pdf");
if (email.get_LastMethodSuccess() == false) {
Log.i(TAG, email.lastErrorText());
return true;
}
Log.i(TAG, "Attached content type = " + contentType);
Log.i(TAG, "NumAttachments = " + String.valueOf(email.get_NumAttachments()));
// Note: The path "qa_data/attachments/report.pdf" is a relative local filesystem path,
// relative to the current working directory of the running application.
}
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."
}
}