Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkMime.pb"
IncludeFile "CkImap.pb"
IncludeFile "CkEmail.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkCert.pb"
IncludeFile "CkStringTable.pb"

Procedure ChilkatExample()

    success.i = 0

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

    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Connect to an IMAP server.
    ; Use TLS
    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)
    success = CkImap::ckConnect(imap,"imap.example.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,"myLogin","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; Select an IMAP mailbox
    success = CkImap::ckSelectMailbox(imap,"Inbox")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; Download the 1st email (as MIME) in the Inbox by sequence number.
    sbMime.i = CkStringBuilder::ckCreate()
    If sbMime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckFetchSingleAsMimeSb(imap,1,0,sbMime)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkStringBuilder::ckDispose(sbMime)
        ProcedureReturn
    EndIf

    ; Load it into a MIME object and check to see if it is signed
    mime.i = CkMime::ckCreate()
    If mime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkMime::ckLoadMimeSb(mime,sbMime)
    alreadySavedParts.i = 0
    If CkMime::ckContainsSignedParts(mime) = 1

        ; This will save the .p7s and other parts...
        st.i = CkStringTable::ckCreate()
        If st.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        success = CkMime::ckPartsToFiles(mime,"c:/temp/qa_output",st)
        If success = 1
            numFiles.i = CkStringTable::ckCount(st)
            i.i = 0
            While i < numFiles
                Debug "Created: " + CkStringTable::ckStringAt(st,i)
                i = i + 1
            Wend
            alreadySavedParts = 1
        EndIf

    EndIf

    ; Load the MIME into an Email object.  This unwraps the security layers and verifies signatures.
    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::ckSetFromMimeSb(email,sbMime)

    If CkEmail::ckReceivedSigned(email) = 1

        Debug "This email was signed."

        ; Check to see if the signatures were verified.
        If CkEmail::ckSignaturesValid(email) = 1
            Debug "Digital signature(s) verified."

            Debug "Signer: " + CkEmail::ckSignedBy(email)

            ; The certificate used for signing may be obtained
            ; by calling email.GetSignedByCert.
            cert.i = CkCert::ckCreate()
            If cert.i = 0
                Debug "Failed to create object."
                ProcedureReturn
            EndIf

            success = CkEmail::ckLastSignerCert(email,i,cert)
            If success = 0
                Debug "Failed to get signing certificate object."
            Else
                Debug "Signing cert: " + CkCert::ckSubjectCN(cert)
            EndIf

        EndIf

    Else
        Debug "Digital signature verification failed."
    EndIf

    If alreadySavedParts <> 1
        CkEmail::ckSaveAllAttachments(email,"c:/temp/qa_output")
    EndIf



    CkImap::ckDispose(imap)
    CkStringBuilder::ckDispose(sbMime)
    CkMime::ckDispose(mime)
    CkStringTable::ckDispose(st)
    CkEmail::ckDispose(email)
    CkCert::ckDispose(cert)


    ProcedureReturn
EndProcedure