Sample code for 30+ languages & platforms
PureBasic

IMAP using SOCKS5, SOCKS4 Proxy

Demonstrates how to connect to an IMAP server through a SOCKS5 or SOCKS4 proxy.

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 SOCKS4 or SOCKS5 proxy, simply set the following
    ; properties prior to connecting with the IMAP server:
    ; The SOCKS hostname may be a domain name or 
    ; IP address:
    CkImap::setCkSocksHostname(imap, "www.mysocksproxyserver.com")
    CkImap::setCkSocksPort(imap, 1080)
    CkImap::setCkSocksUsername(imap, "myProxyLogin")
    CkImap::setCkSocksPassword(imap, "myProxyPassword")
    ; Set the SOCKS version to 4 or 5 based on the version
    ; of the SOCKS proxy server:
    CkImap::setCkSocksVersion(imap, 5)
    ; Note: SOCKS4 servers only support usernames without passwords.
    ; SOCKS5 servers support full login/password authentication.

    ; Connect to an IMAP server through the SOCKS proxy.
    ; Use TLS
    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

    ; 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
    ; We can choose to fetch UIDs or sequence numbers.
    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