Android™
Android™
Get IMAP Attachment Sizes
See more IMAP Examples
Demonstrates the Chilkat Imap.GetMailAttachSize method, which returns the size in bytes of an attachment. The first argument is the Email and the second is the zero-based attachment index. This example fetches headers only and prints each attachment's name and size.
Background: Like the attachment filename, the size comes from
ckx-imap-* metadata and is available from a header-only fetch without downloading the body. A client can therefore show attachment sizes in a message preview, and decide whether to auto-download or prompt before pulling a large file.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;
// Demonstrates the Imap.GetMailAttachSize method, which returns the size in bytes of an
// attachment. The 1st argument is the Email and the 2nd is the zero-based attachment index.
CkImap imap = new CkImap();
imap.put_Ssl(true);
imap.put_Port(993);
success = imap.Connect("imap.example.com");
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
success = imap.Login("user@example.com","myPassword");
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
success = imap.SelectMailbox("Inbox");
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
// Fetch only the message headers. Attachment bodies are NOT downloaded, but the ckx-imap-*
// metadata describing the attachments is present, so the attachment info methods still work.
boolean headersOnly = true;
boolean useUid = false;
int seqNum = 1;
CkEmail email = new CkEmail();
success = imap.FetchEmail(headersOnly,seqNum,useUid,email);
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
int numAttach = imap.GetMailNumAttach(email);
int i;
for (i = 0; i <= numAttach - 1; i++) {
String fname = imap.getMailAttachFilename(email,i);
if (imap.get_LastMethodSuccess() == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
int sz = imap.GetMailAttachSize(email,i);
Log.i(TAG, fname + ": " + String.valueOf(sz) + " bytes");
}
success = imap.Disconnect();
if (success == false) {
Log.i(TAG, imap.lastErrorText());
return;
}
}
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."
}
}