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

C#
bool success = false;

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

Chilkat.Imap imap = new Chilkat.Imap();
imap.Ssl = true;
imap.Port = 993;
imap.RequireSslCertVerify = true;
imap.PeekMode = true;
imap.AutoDownloadAttachments = true;
success = imap.Connect("imap.example.com");
if (success != true) {
    Debug.WriteLine(imap.LastErrorText);
    return;
}

success = imap.Login("IMAP_USERNAME","IMAP_PASSWORD");
if (success != true) {
    Debug.WriteLine(imap.LastErrorText);
    return;
}

success = imap.SelectMailbox("INBOX/postacert");
if (success != true) {
    Debug.WriteLine(imap.LastErrorText);
    return;
}

Chilkat.MessageSet messages = new Chilkat.MessageSet();
success = imap.QueryMbx("SUBJECT \"POSTA CERTIFICATA: Invio File\"",true,messages);
if (success != true) {
    Debug.WriteLine(imap.LastErrorText);
    return;
}

if (messages.Count == 0) {
    Debug.WriteLine("No matching PEC envelope.");
    imap.Disconnect();
    return;
}

Chilkat.Email envelope = new Chilkat.Email();
success = imap.FetchEmail(false,messages.GetId(0),true,envelope);
if (success != true) {
    Debug.WriteLine(imap.LastErrorText);
    return;
}

imap.Disconnect();
Chilkat.Email nested = new Chilkat.Email();
Chilkat.StringBuilder name = new Chilkat.StringBuilder();
int verified = 0;
int i = 0;
while ((i < envelope.NumAttachedMessages)) {
    success = envelope.GetAttachedEmail(i,nested);
    if (success != true) {
        Debug.WriteLine(envelope.LastErrorText);
        return;
    }

    int j = 0;
    while ((j < nested.NumAttachments)) {
        name.SetString(nested.GetAttachmentFilename(j));
        if (name.EndsWith(".xml.p7m",false) == true) {
            Chilkat.BinData data = new Chilkat.BinData();
            success = nested.GetAttachmentBd(j,data);
            if (success != true) {
                Debug.WriteLine(nested.LastErrorText);
                return;
            }

            Chilkat.Crypt2 cms = new Chilkat.Crypt2();
            success = cms.OpaqueVerifyBd(data);
            if (success != true) {
                Debug.WriteLine(cms.LastErrorText);
                return;
            }

            Chilkat.Xml invoice = new Chilkat.Xml();
            success = invoice.LoadBd(data,true);
            if (success != true) {
                Debug.WriteLine(invoice.LastErrorText);
                return;
            }

            Debug.WriteLine(name.GetAsString() + "  XML root: " + invoice.Tag);
            verified = verified + 1;
        }

        j = j + 1;
    }

    i = i + 1;
}

Debug.WriteLine("Verified XML invoices: " + Convert.ToString(verified));