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

Visual Basic 6.0
Dim success As Long
success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

Dim imap As New ChilkatImap
imap.Ssl = 1
imap.Port = 993
imap.RequireSslCertVerify = 1
imap.PeekMode = 1
imap.AutoDownloadAttachments = 1
success = imap.Connect("imap.example.com")
If (success <> 1) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

success = imap.Login("IMAP_USERNAME","IMAP_PASSWORD")
If (success <> 1) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

success = imap.SelectMailbox("INBOX/postacert")
If (success <> 1) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

Dim messages As New MessageSet
success = imap.QueryMbx("SUBJECT ""POSTA CERTIFICATA: Invio File""",1,messages)
If (success <> 1) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

If (messages.Count = 0) Then
    Debug.Print "No matching PEC envelope."
    success = imap.Disconnect()
    Exit Sub
End If

Dim envelope As New ChilkatEmail
success = imap.FetchEmail(0,messages.GetId(0),1,envelope)
If (success <> 1) Then
    Debug.Print imap.LastErrorText
    Exit Sub
End If

success = imap.Disconnect()
Dim nested As New ChilkatEmail
Dim name As New ChilkatStringBuilder
Dim verified As Long
verified = 0
Dim i As Long
i = 0
Do While (i < envelope.NumAttachedMessages)
    success = envelope.GetAttachedEmail(i,nested)
    If (success <> 1) Then
        Debug.Print envelope.LastErrorText
        Exit Sub
    End If

    Dim j As Long
    j = 0
    Do While (j < nested.NumAttachments)
        success = name.SetString(nested.GetAttachmentFilename(j))
        If (name.EndsWith(".xml.p7m",0) = 1) Then
            Dim data As New ChilkatBinData
            success = nested.GetAttachmentBd(j,data)
            If (success <> 1) Then
                Debug.Print nested.LastErrorText
                Exit Sub
            End If

            Dim cms As New ChilkatCrypt2
            success = cms.OpaqueVerifyBd(data)
            If (success <> 1) Then
                Debug.Print cms.LastErrorText
                Exit Sub
            End If

            Dim invoice As New ChilkatXml
            success = invoice.LoadBd(data,1)
            If (success <> 1) Then
                Debug.Print invoice.LastErrorText
                Exit Sub
            End If

            Debug.Print name.GetAsString() & "  XML root: " & invoice.Tag
            verified = verified + 1
        End If

        j = j + 1
    Loop
    i = i + 1
Loop
Debug.Print "Verified XML invoices: " & verified