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

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL oMessages
LOCAL oEnvelope
LOCAL oNested
LOCAL oName
LOCAL nVerified
LOCAL i
LOCAL j
LOCAL oData
LOCAL oCms
LOCAL oInvoice

nSuccess := 0

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

oImap := CreateObject("Chilkat.Imap")
oImap:Ssl := 1
oImap:Port := 993
oImap:RequireSslCertVerify := 1
oImap:PeekMode := 1
oImap:AutoDownloadAttachments := 1
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess != 1)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nSuccess := oImap:Login("IMAP_USERNAME", "IMAP_PASSWORD")
IF (nSuccess != 1)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nSuccess := oImap:SelectMailbox("INBOX/postacert")
IF (nSuccess != 1)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

oMessages := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx('SUBJECT "POSTA CERTIFICATA: Invio File"', 1, oMessages)
IF (nSuccess != 1)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessages:destroy()
    RETURN
ENDIF

IF (oMessages:Count == 0)
    ? "No matching PEC envelope."
    oImap:Disconnect()
    oImap:destroy()
    oMessages:destroy()
    RETURN
ENDIF

oEnvelope := CreateObject("Chilkat.Email")
nSuccess := oImap:FetchEmail(0, oMessages:GetId(0), 1, oEnvelope)
IF (nSuccess != 1)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessages:destroy()
    oEnvelope:destroy()
    RETURN
ENDIF

oImap:Disconnect()
oNested := CreateObject("Chilkat.Email")
oName := CreateObject("Chilkat.StringBuilder")
nVerified := 0
i := 0
DO WHILE (i < oEnvelope:NumAttachedMessages)
    nSuccess := oEnvelope:GetAttachedEmail(i, oNested)
    IF (nSuccess != 1)
        ? oEnvelope:LastErrorText
        oImap:destroy()
        oMessages:destroy()
        oEnvelope:destroy()
        oNested:destroy()
        oName:destroy()
        RETURN
    ENDIF

    j := 0
    DO WHILE (j < oNested:NumAttachments)
        oName:SetString(oNested:GetAttachmentFilename(j))
        IF (oName:EndsWith(".xml.p7m", 0) == 1)
            oData := CreateObject("Chilkat.BinData")
            nSuccess := oNested:GetAttachmentBd(j, oData)
            IF (nSuccess != 1)
                ? oNested:LastErrorText
                oImap:destroy()
                oMessages:destroy()
                oEnvelope:destroy()
                oNested:destroy()
                oName:destroy()
                oData:destroy()
                RETURN
            ENDIF

            oCms := CreateObject("Chilkat.Crypt2")
            nSuccess := oCms:OpaqueVerifyBd(oData)
            IF (nSuccess != 1)
                ? oCms:LastErrorText
                oImap:destroy()
                oMessages:destroy()
                oEnvelope:destroy()
                oNested:destroy()
                oName:destroy()
                oData:destroy()
                oCms:destroy()
                RETURN
            ENDIF

            oInvoice := CreateObject("Chilkat.Xml")
            nSuccess := oInvoice:LoadBd(oData, 1)
            IF (nSuccess != 1)
                ? oInvoice:LastErrorText
                oImap:destroy()
                oMessages:destroy()
                oEnvelope:destroy()
                oNested:destroy()
                oName:destroy()
                oData:destroy()
                oCms:destroy()
                oInvoice:destroy()
                RETURN
            ENDIF

            ? oName:GetAsString() + "  XML root: " + oInvoice:Tag
            nVerified := nVerified + 1
        ENDIF

        j := j + 1
    ENDDO
    i := i + 1
ENDDO
? "Verified XML invoices: " + Str(nVerified)

oImap:destroy()
oMessages:destroy()
oEnvelope:destroy()
oNested:destroy()
oName:destroy()
oData:destroy()
oCms:destroy()
oInvoice:destroy()