AutoIt Requires Chilkat v11.0.0+
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$oImap = ObjCreate("Chilkat.Imap")
$oImap.Ssl = True
$oImap.Port = 993
$oImap.RequireSslCertVerify = True
$oImap.PeekMode = True
$oImap.AutoDownloadAttachments = True
$bSuccess = $oImap.Connect("imap.example.com")
If ($bSuccess <> True) Then
ConsoleWrite($oImap.LastErrorText & @CRLF)
Exit
EndIf
$bSuccess = $oImap.Login("IMAP_USERNAME","IMAP_PASSWORD")
If ($bSuccess <> True) Then
ConsoleWrite($oImap.LastErrorText & @CRLF)
Exit
EndIf
$bSuccess = $oImap.SelectMailbox("INBOX/postacert")
If ($bSuccess <> True) Then
ConsoleWrite($oImap.LastErrorText & @CRLF)
Exit
EndIf
$oMessages = ObjCreate("Chilkat.MessageSet")
$bSuccess = $oImap.QueryMbx("SUBJECT ""POSTA CERTIFICATA: Invio File""",True,$oMessages)
If ($bSuccess <> True) Then
ConsoleWrite($oImap.LastErrorText & @CRLF)
Exit
EndIf
If ($oMessages.Count = 0) Then
ConsoleWrite("No matching PEC envelope." & @CRLF)
$oImap.Disconnect()
Exit
EndIf
$oEnvelope = ObjCreate("Chilkat.Email")
$bSuccess = $oImap.FetchEmail(False,$oMessages.GetId(0),True,$oEnvelope)
If ($bSuccess <> True) Then
ConsoleWrite($oImap.LastErrorText & @CRLF)
Exit
EndIf
$oImap.Disconnect()
$oNested = ObjCreate("Chilkat.Email")
$oName = ObjCreate("Chilkat.StringBuilder")
Local $iVerified = 0
Local $i = 0
While ($i < $oEnvelope.NumAttachedMessages)
$bSuccess = $oEnvelope.GetAttachedEmail($i,$oNested)
If ($bSuccess <> True) Then
ConsoleWrite($oEnvelope.LastErrorText & @CRLF)
Exit
EndIf
Local $iJ = 0
While ($iJ < $oNested.NumAttachments)
$oName.SetString($oNested.GetAttachmentFilename($iJ))
If ($oName.EndsWith(".xml.p7m",False) = True) Then
$oData = ObjCreate("Chilkat.BinData")
$bSuccess = $oNested.GetAttachmentBd($iJ,$oData)
If ($bSuccess <> True) Then
ConsoleWrite($oNested.LastErrorText & @CRLF)
Exit
EndIf
$oCms = ObjCreate("Chilkat.Crypt2")
$bSuccess = $oCms.OpaqueVerifyBd($oData)
If ($bSuccess <> True) Then
ConsoleWrite($oCms.LastErrorText & @CRLF)
Exit
EndIf
$oInvoice = ObjCreate("Chilkat.Xml")
$bSuccess = $oInvoice.LoadBd($oData,True)
If ($bSuccess <> True) Then
ConsoleWrite($oInvoice.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite($oName.GetAsString() & " XML root: " & $oInvoice.Tag & @CRLF)
$iVerified = $iVerified + 1
EndIf
$iJ = $iJ + 1
Wend
$i = $i + 1
Wend
ConsoleWrite("Verified XML invoices: " & $iVerified & @CRLF)