PowerBuilder
PowerBuilder
IMAP Download and Verify Signed MIME
See more IMAP Examples
Downloads the original MIME of a digitally signed email and saves the .p7s signature along with other MIME parts. It then imports the email into a Chilkat email object to unwrap the S/MIME and verify the signature, and subsequently saves the attachments if they haven't been saved already.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Imap
oleobject loo_SbMime
oleobject loo_Mime
integer li_AlreadySavedParts
oleobject loo_St
integer li_NumFiles
integer i
oleobject loo_Email
oleobject loo_Cert
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
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
// Connect to an IMAP server.
// Use TLS
loo_Imap.Ssl = 1
loo_Imap.Port = 993
li_Success = loo_Imap.Connect("imap.example.com")
if li_Success = 0 then
Write-Debug loo_Imap.LastErrorText
destroy loo_Imap
return
end if
li_Success = loo_Imap.Login("myLogin","myPassword")
if li_Success = 0 then
Write-Debug loo_Imap.LastErrorText
destroy loo_Imap
return
end if
// Select an IMAP mailbox
li_Success = loo_Imap.SelectMailbox("Inbox")
if li_Success = 0 then
Write-Debug loo_Imap.LastErrorText
destroy loo_Imap
return
end if
// Download the 1st email (as MIME) in the Inbox by sequence number.
loo_SbMime = create oleobject
li_rc = loo_SbMime.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_Imap.FetchSingleAsMimeSb(1,0,loo_SbMime)
if li_Success = 0 then
Write-Debug loo_Imap.LastErrorText
destroy loo_Imap
destroy loo_SbMime
return
end if
// Load it into a MIME object and check to see if it is signed
loo_Mime = create oleobject
li_rc = loo_Mime.ConnectToNewObject("Chilkat.Mime")
loo_Mime.LoadMimeSb(loo_SbMime)
li_AlreadySavedParts = 0
if loo_Mime.ContainsSignedParts() = 1 then
// This will save the .p7s and other parts...
loo_St = create oleobject
li_rc = loo_St.ConnectToNewObject("Chilkat.StringTable")
li_Success = loo_Mime.PartsToFiles("c:/temp/qa_output",loo_St)
if li_Success = 1 then
li_NumFiles = loo_St.Count
i = 0
do while i < li_NumFiles
Write-Debug "Created: " + loo_St.StringAt(i)
i = i + 1
loop
li_AlreadySavedParts = 1
end if
end if
// Load the MIME into an Email object. This unwraps the security layers and verifies signatures.
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")
loo_Email.SetFromMimeSb(loo_SbMime)
if loo_Email.ReceivedSigned = 1 then
Write-Debug "This email was signed."
// Check to see if the signatures were verified.
if loo_Email.SignaturesValid = 1 then
Write-Debug "Digital signature(s) verified."
Write-Debug "Signer: " + loo_Email.SignedBy
// The certificate used for signing may be obtained
// by calling email.GetSignedByCert.
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")
li_Success = loo_Email.LastSignerCert(i,loo_Cert)
if li_Success = 0 then
Write-Debug "Failed to get signing certificate object."
else
Write-Debug "Signing cert: " + loo_Cert.SubjectCN
end if
end if
else
Write-Debug "Digital signature verification failed."
end if
if li_AlreadySavedParts <> 1 then
loo_Email.SaveAllAttachments("c:/temp/qa_output")
end if
destroy loo_Imap
destroy loo_SbMime
destroy loo_Mime
destroy loo_St
destroy loo_Email
destroy loo_Cert