Sample code for 30+ languages & platforms
Lianja

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

Lianja
llSuccess = .F.

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

loImap = createobject("CkImap")

// Connect to an IMAP server.
// Use TLS
loImap.Ssl = .T.
loImap.Port = 993
llSuccess = loImap.Connect("imap.example.com")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

llSuccess = loImap.Login("myLogin","myPassword")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

// Select an IMAP mailbox
llSuccess = loImap.SelectMailbox("Inbox")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

// Download the 1st email (as MIME) in the Inbox by sequence number.
loSbMime = createobject("CkStringBuilder")
llSuccess = loImap.FetchSingleAsMimeSb(1,.F.,loSbMime)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loSbMime
    return
endif

// Load it into a MIME object and check to see if it is signed
loMime = createobject("CkMime")
loMime.LoadMimeSb(loSbMime)
llAlreadySavedParts = .F.
if (loMime.ContainsSignedParts() = .T.) then

    // This will save the .p7s and other parts...
    loSt = createobject("CkStringTable")
    llSuccess = loMime.PartsToFiles("c:/temp/qa_output",loSt)
    if (llSuccess = .T.) then
        lnNumFiles = loSt.Count
        i = 0
        do while i < lnNumFiles
            ? "Created: " + loSt.StringAt(i)
            i = i + 1
        enddo
        llAlreadySavedParts = .T.
    endif

endif

// Load the MIME into an Email object.  This unwraps the security layers and verifies signatures.
loEmail = createobject("CkEmail")
loEmail.SetFromMimeSb(loSbMime)

if (loEmail.ReceivedSigned = .T.) then

    ? "This email was signed."

    // Check to see if the signatures were verified.
    if (loEmail.SignaturesValid = .T.) then
        ? "Digital signature(s) verified."

        ? "Signer: " + loEmail.SignedBy

        // The certificate used for signing may be obtained
        // by calling email.GetSignedByCert.
        loCert = createobject("CkCert")
        llSuccess = loEmail.LastSignerCert(i,loCert)
        if (llSuccess = .F.) then
            ? "Failed to get signing certificate object."
        else
            ? "Signing cert: " + loCert.SubjectCN
        endif

    endif

else
    ? "Digital signature verification failed."
endif

if (llAlreadySavedParts <> .T.) then
    loEmail.SaveAllAttachments("c:/temp/qa_output")
endif



release loImap
release loSbMime
release loMime
release loSt
release loEmail
release loCert