Xbase++ Requires Chilkat v11.0.0+
Xbase++
Using Client Certificate w/ IMAP SSL
Demonstrates how to use a client-side certificate with an IMAP SSL connection. The SetSslClientCert method is called to specify a certificate to be used for the SSL connection.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oImap
LOCAL oCertStore
LOCAL oJsonCN
LOCAL oCert
LOCAL oCert2
LOCAL nFetchUids
LOCAL oMessageSet
LOCAL oBundle
LOCAL nHeadersOnly
LOCAL oEmail
LOCAL i
LOCAL nNumEmails
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oImap := CreateObject("Chilkat.Imap")
// To use a secure SSL connection, set SSL and the port:
oImap:Ssl := 1
// The typical port for IMAP SSL is 993
oImap:Port := 993
// Load a certificate from a PFX file and use it.
// Note: Other methods are available to load pre-installed
// certificates from registry-based certificate stores.
// Create an instance of a certificate store object, load a PFX file,
// locate the certificate we need, and use it for signing.
// (a PFX file may contain more than one certificate.)
oCertStore := CreateObject("Chilkat.CertStore")
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
nSuccess := oCertStore:LoadPfxFile("myCertWithPrivateKey.pfx", "secret")
IF (nSuccess == 0)
? oCertStore:LastErrorText
oImap:destroy()
oCertStore:destroy()
RETURN
ENDIF
// Find the certificate by the subject common name:
oJsonCN := CreateObject("Chilkat.JsonObject")
oJsonCN:UpdateString("CN", "cert common name")
oCert := CreateObject("Chilkat.Cert")
nSuccess := oCertStore:FindCert(oJsonCN, oCert)
IF (nSuccess == 0)
? oCertStore:LastErrorText
oImap:destroy()
oCertStore:destroy()
oJsonCN:destroy()
oCert:destroy()
RETURN
ENDIF
// If a PFX file is known to contain a single certificate,
// you may load it directly into a Chilkat certificate object.
// This snippet of source code shows how:
oCert2 := CreateObject("Chilkat.Cert")
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
nSuccess := oCert2:LoadPfxFile("myClientCert.pfx", "secret")
IF (nSuccess == 0)
? oCert:LastErrorText
oImap:destroy()
oCertStore:destroy()
oJsonCN:destroy()
oCert:destroy()
oCert2:destroy()
RETURN
ENDIF
// Use the cert:
nSuccess := oImap:SetSslClientCert(oCert)
// Connect to an IMAP server.
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oCertStore:destroy()
oJsonCN:destroy()
oCert:destroy()
oCert2:destroy()
RETURN
ENDIF
// Login
nSuccess := oImap:Login("myLogin", "myPassword")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oCertStore:destroy()
oJsonCN:destroy()
oCert:destroy()
oCert2:destroy()
RETURN
ENDIF
// Select an IMAP mailbox
nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oCertStore:destroy()
oJsonCN:destroy()
oCert:destroy()
oCert2:destroy()
RETURN
ENDIF
// Get the message IDs of all the emails in the mailbox
nFetchUids := 1
oMessageSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("ALL", nFetchUids, oMessageSet)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oCertStore:destroy()
oJsonCN:destroy()
oCert:destroy()
oCert2:destroy()
oMessageSet:destroy()
RETURN
ENDIF
// Fetch the emails into a bundle object:
oBundle := CreateObject("Chilkat.EmailBundle")
nHeadersOnly := 0
nSuccess := oImap:FetchMsgSet(nHeadersOnly, oMessageSet, oBundle)
IF (nSuccess == 0)
? oImap:LastErrorText
oImap:destroy()
oCertStore:destroy()
oJsonCN:destroy()
oCert:destroy()
oCert2:destroy()
oMessageSet:destroy()
oBundle:destroy()
RETURN
ENDIF
// Loop over the bundle and display the FROM and SUBJECT of each.
oEmail := CreateObject("Chilkat.Email")
i := 0
nNumEmails := oBundle:MessageCount
DO WHILE i < nNumEmails
oBundle:EmailAt(i, oEmail)
? oEmail:From
? oEmail:Subject
? "--"
i := i + 1
ENDDO
// Disconnect from the IMAP server.
nSuccess := oImap:Disconnect()
oImap:destroy()
oCertStore:destroy()
oJsonCN:destroy()
oCert:destroy()
oCert2:destroy()
oMessageSet:destroy()
oBundle:destroy()
oEmail:destroy()