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

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL oSbMime
LOCAL oMime
LOCAL nAlreadySavedParts
LOCAL oSt
LOCAL nNumFiles
LOCAL i
LOCAL oEmail
LOCAL oCert

nSuccess := 0

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

oImap := CreateObject("Chilkat.Imap")

//  Connect to an IMAP server.
//  Use TLS
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nSuccess := oImap:Login("myLogin", "myPassword")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Select an IMAP mailbox
nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Download the 1st email (as MIME) in the Inbox by sequence number.
oSbMime := CreateObject("Chilkat.StringBuilder")
nSuccess := oImap:FetchSingleAsMimeSb(1, 0, oSbMime)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oSbMime:destroy()
    RETURN
ENDIF

//  Load it into a MIME object and check to see if it is signed
oMime := CreateObject("Chilkat.Mime")
oMime:LoadMimeSb(oSbMime)
nAlreadySavedParts := 0
IF (oMime:ContainsSignedParts() == 1)

    //  This will save the .p7s and other parts...
    oSt := CreateObject("Chilkat.StringTable")
    nSuccess := oMime:PartsToFiles("c:/temp/qa_output", oSt)
    IF (nSuccess == 1)
        nNumFiles := oSt:Count
        i := 0
        DO WHILE i < nNumFiles
            ? "Created: " + oSt:StringAt(i)
            i := i + 1
        ENDDO
        nAlreadySavedParts := 1
    ENDIF

ENDIF

//  Load the MIME into an Email object.  This unwraps the security layers and verifies signatures.
oEmail := CreateObject("Chilkat.Email")
oEmail:SetFromMimeSb(oSbMime)

IF (oEmail:ReceivedSigned == 1)

    ? "This email was signed."

    //  Check to see if the signatures were verified.
    IF (oEmail:SignaturesValid == 1)
        ? "Digital signature(s) verified."

        ? "Signer: " + oEmail:SignedBy

        //  The certificate used for signing may be obtained
        //  by calling email.GetSignedByCert.
        oCert := CreateObject("Chilkat.Cert")
        nSuccess := oEmail:LastSignerCert(i, oCert)
        IF (nSuccess == 0)
            ? "Failed to get signing certificate object."
        ELSE
            ? "Signing cert: " + oCert:SubjectCN
        ENDIF

    ENDIF

ELSE
    ? "Digital signature verification failed."
ENDIF

IF (nAlreadySavedParts != 1)
    oEmail:SaveAllAttachments("c:/temp/qa_output")
ENDIF

oImap:destroy()
oSbMime:destroy()
oMime:destroy()
oSt:destroy()
oEmail:destroy()
oCert:destroy()