PowerBuilder Requires Chilkat v11.0.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Imap
oleobject loo_Messages
oleobject loo_Envelope
oleobject loo_Nested
oleobject loo_Name
long li_Verified
long i
long j
oleobject loo_Data
oleobject loo_Cms
oleobject loo_Invoice
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
destroy loo_Imap
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Imap.Ssl = 1
loo_Imap.Port = 993
loo_Imap.RequireSslCertVerify = 1
loo_Imap.PeekMode = 1
loo_Imap.AutoDownloadAttachments = 1
li_Success = loo_Imap.Connect("imap.example.com")
if li_Success <> 1 then
Write-Debug loo_Imap.LastErrorText
destroy loo_Imap
return
end if
li_Success = loo_Imap.Login("IMAP_USERNAME","IMAP_PASSWORD")
if li_Success <> 1 then
Write-Debug loo_Imap.LastErrorText
destroy loo_Imap
return
end if
li_Success = loo_Imap.SelectMailbox("INBOX/postacert")
if li_Success <> 1 then
Write-Debug loo_Imap.LastErrorText
destroy loo_Imap
return
end if
loo_Messages = create oleobject
li_rc = loo_Messages.ConnectToNewObject("Chilkat.MessageSet")
li_Success = loo_Imap.QueryMbx("SUBJECT ~"POSTA CERTIFICATA: Invio File~"",1,loo_Messages)
if li_Success <> 1 then
Write-Debug loo_Imap.LastErrorText
destroy loo_Imap
destroy loo_Messages
return
end if
if loo_Messages.Count = 0 then
Write-Debug "No matching PEC envelope."
loo_Imap.Disconnect()
destroy loo_Imap
destroy loo_Messages
return
end if
loo_Envelope = create oleobject
li_rc = loo_Envelope.ConnectToNewObject("Chilkat.Email")
li_Success = loo_Imap.FetchEmail(0,loo_Messages.GetId(0),1,loo_Envelope)
if li_Success <> 1 then
Write-Debug loo_Imap.LastErrorText
destroy loo_Imap
destroy loo_Messages
destroy loo_Envelope
return
end if
loo_Imap.Disconnect()
loo_Nested = create oleobject
li_rc = loo_Nested.ConnectToNewObject("Chilkat.Email")
loo_Name = create oleobject
li_rc = loo_Name.ConnectToNewObject("Chilkat.StringBuilder")
li_Verified = 0
i = 0
do while (i < loo_Envelope.NumAttachedMessages)
li_Success = loo_Envelope.GetAttachedEmail(i,loo_Nested)
if li_Success <> 1 then
Write-Debug loo_Envelope.LastErrorText
destroy loo_Imap
destroy loo_Messages
destroy loo_Envelope
destroy loo_Nested
destroy loo_Name
return
end if
j = 0
do while (j < loo_Nested.NumAttachments)
loo_Name.SetString(loo_Nested.GetAttachmentFilename(j))
if loo_Name.EndsWith(".xml.p7m",0) = 1 then
loo_Data = create oleobject
li_rc = loo_Data.ConnectToNewObject("Chilkat.BinData")
li_Success = loo_Nested.GetAttachmentBd(j,loo_Data)
if li_Success <> 1 then
Write-Debug loo_Nested.LastErrorText
destroy loo_Imap
destroy loo_Messages
destroy loo_Envelope
destroy loo_Nested
destroy loo_Name
destroy loo_Data
return
end if
loo_Cms = create oleobject
li_rc = loo_Cms.ConnectToNewObject("Chilkat.Crypt2")
li_Success = loo_Cms.OpaqueVerifyBd(loo_Data)
if li_Success <> 1 then
Write-Debug loo_Cms.LastErrorText
destroy loo_Imap
destroy loo_Messages
destroy loo_Envelope
destroy loo_Nested
destroy loo_Name
destroy loo_Data
destroy loo_Cms
return
end if
loo_Invoice = create oleobject
li_rc = loo_Invoice.ConnectToNewObject("Chilkat.Xml")
li_Success = loo_Invoice.LoadBd(loo_Data,1)
if li_Success <> 1 then
Write-Debug loo_Invoice.LastErrorText
destroy loo_Imap
destroy loo_Messages
destroy loo_Envelope
destroy loo_Nested
destroy loo_Name
destroy loo_Data
destroy loo_Cms
destroy loo_Invoice
return
end if
Write-Debug loo_Name.GetAsString() + " XML root: " + loo_Invoice.Tag
li_Verified = li_Verified + 1
end if
j = j + 1
loop
i = i + 1
loop
Write-Debug "Verified XML invoices: " + string(li_Verified)
destroy loo_Imap
destroy loo_Messages
destroy loo_Envelope
destroy loo_Nested
destroy loo_Name
destroy loo_Data
destroy loo_Cms
destroy loo_Invoice