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

Xojo Plugin
Dim success As Boolean
success = False

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

Dim imap As 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) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

success = imap.Login("IMAP_USERNAME","IMAP_PASSWORD")
If (success <> True) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

success = imap.SelectMailbox("INBOX/postacert")
If (success <> True) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

Dim messages As New Chilkat.MessageSet
success = imap.QueryMbx("SUBJECT ""POSTA CERTIFICATA: Invio File""",True,messages)
If (success <> True) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

If (messages.Count = 0) Then
    System.DebugLog("No matching PEC envelope.")
    success = imap.Disconnect()
    Return
End If

Dim envelope As New Chilkat.Email
success = imap.FetchEmail(False,messages.GetId(0),True,envelope)
If (success <> True) Then
    System.DebugLog(imap.LastErrorText)
    Return
End If

success = imap.Disconnect()
Dim nested As New Chilkat.Email
Dim name As New Chilkat.StringBuilder
Dim verified As Int32
verified = 0
Dim i As Int32
i = 0
While (i < envelope.NumAttachedMessages)
    success = envelope.GetAttachedEmail(i,nested)
    If (success <> True) Then
        System.DebugLog(envelope.LastErrorText)
        Return
    End If

    Dim j As Int32
    j = 0
    While (j < nested.NumAttachments)
        success = name.SetString(nested.GetAttachmentFilename(j))
        If (name.EndsWith(".xml.p7m",False) = True) Then
            Dim data As New Chilkat.BinData
            success = nested.GetAttachmentBd(j,data)
            If (success <> True) Then
                System.DebugLog(nested.LastErrorText)
                Return
            End If

            Dim cms As New Chilkat.Crypt2
            success = cms.OpaqueVerifyBd(data)
            If (success <> True) Then
                System.DebugLog(cms.LastErrorText)
                Return
            End If

            Dim invoice As New Chilkat.Xml
            success = invoice.LoadBd(data,True)
            If (success <> True) Then
                System.DebugLog(invoice.LastErrorText)
                Return
            End If

            System.DebugLog(name.GetAsString() + "  XML root: " + invoice.Tag)
            verified = verified + 1
        End If

        j = j + 1
    Wend
    i = i + 1
Wend
System.DebugLog("Verified XML invoices: " + Str(verified))