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

VB.NET
Dim success As Boolean = 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
    Debug.WriteLine(imap.LastErrorText)
    Exit Sub
End If

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

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

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

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

Dim envelope As New Chilkat.Email
success = imap.FetchEmail(False,messages.GetId(0),True,envelope)
If (success <> True) Then
    Debug.WriteLine(imap.LastErrorText)
    Exit Sub
End If

imap.Disconnect()
Dim nested As New Chilkat.Email
Dim name As New Chilkat.StringBuilder
Dim verified As Integer = 0
Dim i As Integer = 0
While (i < envelope.NumAttachedMessages)
    success = envelope.GetAttachedEmail(i,nested)
    If (success <> True) Then
        Debug.WriteLine(envelope.LastErrorText)
        Exit Sub
    End If

    Dim j As Integer = 0
    While (j < nested.NumAttachments)
        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
                Debug.WriteLine(nested.LastErrorText)
                Exit Sub
            End If

            Dim cms As New Chilkat.Crypt2
            success = cms.OpaqueVerifyBd(data)
            If (success <> True) Then
                Debug.WriteLine(cms.LastErrorText)
                Exit Sub
            End If

            Dim invoice As New Chilkat.Xml
            success = invoice.LoadBd(data,True)
            If (success <> True) Then
                Debug.WriteLine(invoice.LastErrorText)
                Exit Sub
            End If

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

        j = j + 1
    End While
    i = i + 1
End While
Debug.WriteLine("Verified XML invoices: " & verified)