Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loImap
LOCAL loSbMime
LOCAL loMime
LOCAL lnAlreadySavedParts
LOCAL loSt
LOCAL lnNumFiles
LOCAL i
LOCAL loEmail
LOCAL loCert

lnSuccess = 0

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

loImap = CreateObject('Chilkat.Imap')

* Connect to an IMAP server.
* Use TLS
loImap.Ssl = 1
loImap.Port = 993
lnSuccess = loImap.Connect("imap.example.com")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.Login("myLogin","myPassword")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

* Select an IMAP mailbox
lnSuccess = loImap.SelectMailbox("Inbox")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

* Download the 1st email (as MIME) in the Inbox by sequence number.
loSbMime = CreateObject('Chilkat.StringBuilder')
lnSuccess = loImap.FetchSingleAsMimeSb(1,0,loSbMime)
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loSbMime
    CANCEL
ENDIF

* Load it into a MIME object and check to see if it is signed
loMime = CreateObject('Chilkat.Mime')
loMime.LoadMimeSb(loSbMime)
lnAlreadySavedParts = 0
IF (loMime.ContainsSignedParts() = 1) THEN

    * This will save the .p7s and other parts...
    loSt = CreateObject('Chilkat.StringTable')
    lnSuccess = loMime.PartsToFiles("c:/temp/qa_output",loSt)
    IF (lnSuccess = 1) THEN
        lnNumFiles = loSt.Count
        i = 0
        DO WHILE i < lnNumFiles
            ? "Created: " + loSt.StringAt(i)
            i = i + 1
        ENDDO
        lnAlreadySavedParts = 1
    ENDIF

ENDIF

* Load the MIME into an Email object.  This unwraps the security layers and verifies signatures.
loEmail = CreateObject('Chilkat.Email')
loEmail.SetFromMimeSb(loSbMime)

IF (loEmail.ReceivedSigned = 1) THEN

    ? "This email was signed."

    * Check to see if the signatures were verified.
    IF (loEmail.SignaturesValid = 1) THEN
        ? "Digital signature(s) verified."

        ? "Signer: " + loEmail.SignedBy

        * The certificate used for signing may be obtained
        * by calling email.GetSignedByCert.
        loCert = CreateObject('Chilkat.Cert')
        lnSuccess = loEmail.LastSignerCert(i,loCert)
        IF (lnSuccess = 0) THEN
            ? "Failed to get signing certificate object."
        ELSE
            ? "Signing cert: " + loCert.SubjectCN
        ENDIF

    ENDIF

ELSE
    ? "Digital signature verification failed."
ENDIF

IF (lnAlreadySavedParts <> 1) THEN
    loEmail.SaveAllAttachments("c:/temp/qa_output")
ENDIF

RELEASE loImap
RELEASE loSbMime
RELEASE loMime
RELEASE loSt
RELEASE loEmail
RELEASE loCert