Sample code for 30+ languages & platforms
Android™ Requires Chilkat v11.0.0+

Preview POP3 messages and download a complete email

See more Email Object Examples

Read a short preview for each of up to five messages into an EmailBundle, print their subjects, and download the complete first entry into a separate Email. Configure a POP3 account that supports UIDL and TOP. All messages stay on the server.

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;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    CkMailMan mail = new CkMailMan();
    mail.put_MailHost("pop.example.com");
    mail.put_MailPort(995);
    mail.put_PopSsl(true);
    mail.put_RequireSslCertVerify(true);
    mail.put_PopUsername("MAIL_USER");
    mail.put_PopPassword("MAIL_PASSWORD");
    CkEmailBundle previews = new CkEmailBundle();
    //  Keep the messages and request minimal previews. Range indexes are zero-based and inclusive.
    success = mail.FetchRange(true,true,0,0,4,previews);
    if (success != true) {
        Log.i(TAG, mail.lastErrorText());
        return;
        }

    if (previews.get_MessageCount() == 0) {
        Log.i(TAG, "There are no messages to preview.");
        success = mail.Pop3EndSession();
        if (success != true) {
            Log.i(TAG, mail.lastErrorText());
            return;
            }

        return;
        }

    CkEmail header = new CkEmail();
    int i = 0;
    while ((i < previews.get_MessageCount())) {
        success = previews.EmailAt(i,header);
        if (success != true) {
            Log.i(TAG, previews.lastErrorText());
            return;
            }

        Log.i(TAG, header.uidl() + "  " + header.subject());
        i = i + 1;
        }

    //  A UI could use its selected row here. FetchFull uses the preview's POP3 identity.
    success = previews.EmailAt(0,header);
    if (success != true) {
        Log.i(TAG, previews.lastErrorText());
        return;
        }

    CkEmail email = new CkEmail();
    success = mail.FetchFull(header,email);
    if (success != true) {
        Log.i(TAG, mail.lastErrorText());
        return;
        }

    success = mail.Pop3EndSession();
    if (success != true) {
        Log.i(TAG, mail.lastErrorText());
        return;
        }

    Log.i(TAG, "Full message: " + email.subject() + "  Files: " + String.valueOf(email.get_NumAttachments()));

    success = email.SaveEml("pop-received.eml");
    if (success != true) {
        Log.i(TAG, email.lastErrorText());
        return;
        }

    Log.i(TAG, "Saved pop-received.eml.");

  }

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