Sample code for 30+ languages & platforms
B4X Requires Chilkat v11.0.0+

IMAP Download and Verify Signed (S/MIME) Email

See more IMAP Examples

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

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

'  This example requires the Chilkat API to have been previously unlocked.
'  See Global Unlock Sample for sample code.

Dim imap As ChilkatImap
imap.Initialize("imap")

'  Connect to an IMAP server.
'  Use TLS
imap.Ssl = True
imap.Port = 993
success = imap.Connect("imap.example.com")
If success = False Then
    Log(imap.LastErrorText)
    Return
End If


success = imap.Login("myLogin", "myPassword")
If success = False Then
    Log(imap.LastErrorText)
    Return
End If


'  Select an IMAP mailbox
success = imap.SelectMailbox("Inbox")
If success = False Then
    Log(imap.LastErrorText)
    Return
End If


'  We can choose to fetch UIDs or sequence numbers.
Dim fetchUids As Boolean = True

'  Get the message IDs of all the emails in the mailbox
Dim messageSet As ChilkatMessageSet
messageSet.Initialize
success = imap.QueryMbx("ALL", fetchUids, messageSet)
If success = False Then
    Log(imap.LastErrorText)
    Return
End If


Dim email As ChilkatEmail
email.Initialize
Dim cert As ChilkatCert
cert.Initialize("cert")

Dim i As Int = 0
Do While i < messageSet.Count

    Dim uid As String = messageSet.GetId(i)
    Log("uid: " & uid)

    success = imap.FetchEmail(False, uid, True, email)
    If success = False Then
        Log(imap.LastErrorText)
        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 email.ReceivedSigned = True Then

        Log("This email was signed.")

        '  Check to see if the signatures were verified.
        If email.SignaturesValid = True Then
            Log("Digital signature(s) verified.")
            Log("Signer: " & email.SignedBy)

            '  Get the certificate used for signing.
            success = email.LastSignerCert(0, cert)

            If success = False Then
                Log("Failed to get signing certificate object.")
            Else
                Log("Signing cert: " & cert.SubjectCN)
            End If


        Else
            Log("Digital signature verification failed.")
        End If


    End If


    i = i + 1
Loop

'  Disconnect from the IMAP server.
success = imap.Disconnect