Sample code for 30+ languages & platforms
PureBasic

Secure IMAP over TLS/SSL

To communicate with an IMAP server over TLS/SSL, simply set the Ssl and Port properties. The remainder of your code is the same as with non-SSL.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkImap.pb"
IncludeFile "CkEmailBundle.pb"
IncludeFile "CkMessageSet.pb"
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

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

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

    ; To use a secure TLS/SSL connection, set the Ssl property and the port:
    CkImap::setCkSsl(imap, 1)
    ; The typical port for IMAP SSL is 993
    CkImap::setCkPort(imap, 993)

    ; Connect to an IMAP server.
    success = CkImap::ckConnect(imap,"imap.example.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; Login
    success = CkImap::ckLogin(imap,"myLogin","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; Select an IMAP mailbox
    success = CkImap::ckSelectMailbox(imap,"Inbox")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; Get the message IDs of all the emails in the mailbox
    fetchUids.i = 1
    messageSet.i = CkMessageSet::ckCreate()
    If messageSet.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckQueryMbx(imap,"ALL",fetchUids,messageSet)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(messageSet)
        ProcedureReturn
    EndIf

    ; Fetch the emails into a bundle object:
    bundle.i = CkEmailBundle::ckCreate()
    If bundle.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    headersOnly.i = 0
    success = CkImap::ckFetchMsgSet(imap,headersOnly,messageSet,bundle)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(messageSet)
        CkEmailBundle::ckDispose(bundle)
        ProcedureReturn
    EndIf

    ; Loop over the bundle and display the FROM and SUBJECT of each.
    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    i.i = 0
    numEmails.i = CkEmailBundle::ckMessageCount(bundle)
    While i < numEmails
        CkEmailBundle::ckEmailAt(bundle,i,email)

        Debug CkEmail::ckFrom(email)
        Debug CkEmail::ckSubject(email)
        Debug "--"
        i = i + 1
    Wend

    ; Disconnect from the IMAP server.
    success = CkImap::ckDisconnect(imap)


    CkImap::ckDispose(imap)
    CkMessageSet::ckDispose(messageSet)
    CkEmailBundle::ckDispose(bundle)
    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure