Sample code for 30+ languages & platforms
Android™

IMAP Download and Verify Signed MIME

See more IMAP Examples

Downloads the original MIME of a digitally signed email and saves the .p7s signature along with other MIME parts. It then imports the email into a Chilkat email object to unwrap the S/MIME and verify the signature, and subsequently saves the attachments if they haven't been saved already.

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;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkImap imap = new CkImap();

    // Connect to an IMAP server.
    // Use TLS
    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("myLogin","myPassword");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    // Select an IMAP mailbox
    success = imap.SelectMailbox("Inbox");
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    // Download the 1st email (as MIME) in the Inbox by sequence number.
    CkStringBuilder sbMime = new CkStringBuilder();
    success = imap.FetchSingleAsMimeSb(1,false,sbMime);
    if (success == false) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    // Load it into a MIME object and check to see if it is signed
    CkMime mime = new CkMime();
    mime.LoadMimeSb(sbMime);
    boolean alreadySavedParts = false;
    if (mime.ContainsSignedParts() == true) {

        // This will save the .p7s and other parts...
        CkStringTable st = new CkStringTable();
        success = mime.PartsToFiles("c:/temp/qa_output",st);
        if (success == true) {
            int numFiles = st.get_Count();
            int i = 0;
            while (i < numFiles) {
                Log.i(TAG, "Created: " + st.stringAt(i));
                i = i + 1;
                }

            alreadySavedParts = true;
            }

        }

    // Load the MIME into an Email object.  This unwraps the security layers and verifies signatures.
    CkEmail email = new CkEmail();
    email.SetFromMimeSb(sbMime);

    if (email.get_ReceivedSigned() == true) {

        Log.i(TAG, "This email was signed.");

        // Check to see if the signatures were verified.
        if (email.get_SignaturesValid() == true) {
            Log.i(TAG, "Digital signature(s) verified.");

            Log.i(TAG, "Signer: " + email.signedBy());

            // The certificate used for signing may be obtained
            // by calling email.GetSignedByCert.
            CkCert cert = new CkCert();
            success = email.LastSignerCert(i,cert);
            if (success == false) {
                Log.i(TAG, "Failed to get signing certificate object.");
                }
            else {
                Log.i(TAG, "Signing cert: " + cert.subjectCN());
                }

            }

        }
    else {
        Log.i(TAG, "Digital signature verification failed.");
        }

    if (alreadySavedParts != true) {
        email.SaveAllAttachments("c:/temp/qa_output");
        }


  }

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