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

Extract signed invoice XML from an Italian PEC message

See more Email Object Examples

Search the PEC folder by subject, fetch one envelope by UID, and inspect the attachments of each nested email. Match .xml.p7m by filename and verify the opaque CMS signature before loading the XML. This checks signature integrity. Certificate trust and invoice schema validation are separate application decisions.

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.

    CkImap imap = new CkImap();
    imap.put_Ssl(true);
    imap.put_Port(993);
    imap.put_RequireSslCertVerify(true);
    imap.put_PeekMode(true);
    imap.put_AutoDownloadAttachments(true);
    success = imap.Connect("imap.example.com");
    if (success != true) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    success = imap.Login("IMAP_USERNAME","IMAP_PASSWORD");
    if (success != true) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    success = imap.SelectMailbox("INBOX/postacert");
    if (success != true) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    CkMessageSet messages = new CkMessageSet();
    success = imap.QueryMbx("SUBJECT \"POSTA CERTIFICATA: Invio File\"",true,messages);
    if (success != true) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    if (messages.get_Count() == 0) {
        Log.i(TAG, "No matching PEC envelope.");
        imap.Disconnect();
        return;
        }

    CkEmail envelope = new CkEmail();
    success = imap.FetchEmail(false,messages.GetId(0),true,envelope);
    if (success != true) {
        Log.i(TAG, imap.lastErrorText());
        return;
        }

    imap.Disconnect();
    CkEmail nested = new CkEmail();
    CkStringBuilder name = new CkStringBuilder();
    int verified = 0;
    int i = 0;
    while ((i < envelope.get_NumAttachedMessages())) {
        success = envelope.GetAttachedEmail(i,nested);
        if (success != true) {
            Log.i(TAG, envelope.lastErrorText());
            return;
            }

        int j = 0;
        while ((j < nested.get_NumAttachments())) {
            name.SetString(nested.getAttachmentFilename(j));
            if (name.EndsWith(".xml.p7m",false) == true) {
                CkBinData data = new CkBinData();
                success = nested.GetAttachmentBd(j,data);
                if (success != true) {
                    Log.i(TAG, nested.lastErrorText());
                    return;
                    }

                CkCrypt2 cms = new CkCrypt2();
                success = cms.OpaqueVerifyBd(data);
                if (success != true) {
                    Log.i(TAG, cms.lastErrorText());
                    return;
                    }

                CkXml invoice = new CkXml();
                success = invoice.LoadBd(data,true);
                if (success != true) {
                    Log.i(TAG, invoice.lastErrorText());
                    return;
                    }

                Log.i(TAG, name.getAsString() + "  XML root: " + invoice.tag());
                verified = verified + 1;
                }

            j = j + 1;
            }

        i = i + 1;
        }

    Log.i(TAG, "Verified XML invoices: " + String.valueOf(verified));

  }

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