Sample code for 30+ languages & platforms
Java 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 Java Downloads

Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.MessageSet;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.Xml;

public class ChilkatExample {

    public static void main(String[] args) {
        // Assumes Chilkat is unlocked. See Global Unlock Sample.

        Imap imap = new Imap();
        imap.setSsl(true);
        imap.setPort(993);
        imap.setRequireSslCertVerify(true);
        imap.setPeekMode(true);
        imap.setAutoDownloadAttachments(true);
        try {
            imap.connect("imap.example.com");

            imap.login("IMAP_USERNAME", "IMAP_PASSWORD");

            imap.selectMailbox("INBOX/postacert");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        MessageSet messages = new MessageSet();
        try {
            imap.queryMbx("SUBJECT \"POSTA CERTIFICATA: Invio File\"", true, messages);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        if (messages.getCount() == 0) {
            System.out.println("No matching PEC envelope.");
            imap.disconnect();
            return;
        }

        Email envelope = new Email();
        try {
            imap.fetchEmail(false, messages.getId(0), true, envelope);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        imap.disconnect();
        Email nested = new Email();
        StringBuilder name = new StringBuilder();
        int verified = 0;
        int i = 0;
        while (i < envelope.getNumAttachedMessages()) {
            try {
                envelope.getAttachedEmail(i, nested);
            } catch (ChilkatException e) {
                System.out.println(e.getLastErrorText());
                return;
            }

            int j = 0;
            while (j < nested.getNumAttachments()) {
                name.setString(nested.getAttachmentFilename(j));
                if (name.endsWith(".xml.p7m", false)) {
                    BinData data = new BinData();
                    try {
                        nested.getAttachmentBd(j, data);
                    } catch (ChilkatException e) {
                        System.out.println(e.getLastErrorText());
                        return;
                    }

                    Crypt2 cms = new Crypt2();
                    try {
                        cms.opaqueVerifyBd(data);
                    } catch (ChilkatException e) {
                        System.out.println(e.getLastErrorText());
                        return;
                    }

                    Xml invoice = new Xml();
                    try {
                        invoice.loadBd(data, true);
                    } catch (ChilkatException e) {
                        System.out.println(e.getLastErrorText());
                        return;
                    }

                    System.out.println(name.getAsString() + "  XML root: " + invoice.getTag());
                    verified = verified + 1;
                }

                j = j + 1;
            }

            i = i + 1;
        }

        System.out.println("Verified XML invoices: " + verified);
    }
}