Sample code for 30+ languages & platforms
PowerBuilder

IMAP Download and Verify Signed (S/MIME) Email

See more IMAP Examples

Demonstrates how to download and verify digitally signed S/MIME email.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
integer li_FetchUids
oleobject loo_MessageSet
oleobject loo_Email
oleobject loo_Cert
integer i
string ls_Uid

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

// We can choose to fetch UIDs or sequence numbers.
li_FetchUids = 1

// Get the message IDs of all the emails in the mailbox
loo_MessageSet = create oleobject
li_rc = loo_MessageSet.ConnectToNewObject("Chilkat.MessageSet")

li_Success = loo_Imap.QueryMbx("ALL",li_FetchUids,loo_MessageSet)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_MessageSet
    return
end if

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

i = 0
do while i < loo_MessageSet.Count

    ls_Uid = loo_MessageSet.GetId(i)
    Write-Debug "uid: " + ls_Uid

    li_Success = loo_Imap.FetchEmail(0,ls_Uid,1,loo_Email)
    if li_Success = 0 then
        Write-Debug loo_Imap.LastErrorText
        destroy loo_Imap
        destroy loo_MessageSet
        destroy loo_Email
        destroy loo_Cert
        return
    end if

    // The security layers of signed and/or encrypted emails
    // are automatically "unwrapped" when loaded into
    // a Chilkat email object.
    // An application only needs to check to see if an email
    // was received signed or encrypted, and then examine
    // the success/failure.  For example:
    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

            // Get the certificate used for signing.
            li_Success = loo_Email.LastSignerCert(0,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

        else
            Write-Debug "Digital signature verification failed."
        end if

    end if

    i = i + 1
loop

// Disconnect from the IMAP server.
li_Success = loo_Imap.Disconnect()


destroy loo_Imap
destroy loo_MessageSet
destroy loo_Email
destroy loo_Cert