Sample code for 30+ languages & platforms
PureBasic

Use an XML Certificate Vault with IMAP

See more IMAP Examples

Demonstrates the Chilkat Imap.UseCertVault method, which associates an XmlCertVault with the Imap object as a source of certificates and private keys for S/MIME operations. The only argument is the vault. Only one vault is associated at a time.

Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.

Background: An XmlCertVault is a portable container that can hold multiple certificates and keys in a single (optionally password-protected) XML file. It is a convenient way to ship the exact set of credentials an application needs without depending on the operating system's certificate stores. Associating a vault makes all of its contents available for S/MIME decryption and signature verification.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkXmlCertVault.pb"
IncludeFile "CkImap.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Imap.UseCertVault method, which associates an XmlCertVault with the Imap
    ;  object as a source of certificates and private keys for S/MIME operations.  The only
    ;  argument is the XmlCertVault.

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

    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,"user@example.com","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ;  The PFX password is not hard-coded in source.  Insert code here to obtain it from an
    ;  interactive prompt, environment variable, or a secrets vault.
    pfxPassword.s

    ;  Build a certificate vault and add a PFX to it.
    vault.i = CkXmlCertVault::ckCreate()
    If vault.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkXmlCertVault::ckAddPfxFile(vault,"qa_data/smime.pfx",pfxPassword)
    If success = 0
        Debug CkXmlCertVault::ckLastErrorText(vault)
        CkImap::ckDispose(imap)
        CkXmlCertVault::ckDispose(vault)
        ProcedureReturn
    EndIf

    ;  Associate the vault with the Imap object.  Only one vault is associated at a time.
    success = CkImap::ckUseCertVault(imap,vault)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkXmlCertVault::ckDispose(vault)
        ProcedureReturn
    EndIf

    success = CkImap::ckDisconnect(imap)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkXmlCertVault::ckDispose(vault)
        ProcedureReturn
    EndIf



    CkImap::ckDispose(imap)
    CkXmlCertVault::ckDispose(vault)


    ProcedureReturn
EndProcedure