Sample code for 30+ languages & platforms
Unicode C

IMAP using SOCKS5, SOCKS4 Proxy

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

Chilkat Unicode C Downloads

Unicode C
#include <C_CkImapW.h>
#include <C_CkMessageSetW.h>
#include <C_CkEmailBundleW.h>
#include <C_CkEmailW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imap;
    BOOL fetchUids;
    HCkMessageSetW messageSet;
    HCkEmailBundleW bundle;
    BOOL headersOnly;
    HCkEmailW email;
    int i;
    int numEmails;

    success = FALSE;

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

    imap = CkImapW_Create();

    // 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:
    CkImapW_putSocksHostname(imap,L"www.mysocksproxyserver.com");
    CkImapW_putSocksPort(imap,1080);
    CkImapW_putSocksUsername(imap,L"myProxyLogin");
    CkImapW_putSocksPassword(imap,L"myProxyPassword");
    // Set the SOCKS version to 4 or 5 based on the version
    // of the SOCKS proxy server:
    CkImapW_putSocksVersion(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
    CkImapW_putSsl(imap,TRUE);
    CkImapW_putPort(imap,993);
    success = CkImapW_Connect(imap,L"imap.example.com");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    // Login
    success = CkImapW_Login(imap,L"myLogin",L"myPassword");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    // Select an IMAP mailbox
    success = CkImapW_SelectMailbox(imap,L"Inbox");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    // Get the message IDs of all the emails in the mailbox
    // We can choose to fetch UIDs or sequence numbers.
    fetchUids = TRUE;
    messageSet = CkMessageSetW_Create();
    success = CkImapW_QueryMbx(imap,L"ALL",fetchUids,messageSet);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkMessageSetW_Dispose(messageSet);
        return;
    }

    // Fetch the emails into a bundle object:
    bundle = CkEmailBundleW_Create();
    headersOnly = FALSE;
    success = CkImapW_FetchMsgSet(imap,headersOnly,messageSet,bundle);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkMessageSetW_Dispose(messageSet);
        CkEmailBundleW_Dispose(bundle);
        return;
    }

    // Loop over the bundle and display the FROM and SUBJECT of each.
    email = CkEmailW_Create();
    i = 0;
    numEmails = CkEmailBundleW_getMessageCount(bundle);
    while (i < numEmails) {

        CkEmailBundleW_EmailAt(bundle,i,email);

        wprintf(L"%s\n",CkEmailW_ck_from(email));
        wprintf(L"%s\n",CkEmailW_subject(email));
        wprintf(L"--\n");
        i = i + 1;
    }

    // Disconnect from the IMAP server.
    success = CkImapW_Disconnect(imap);


    CkImapW_Dispose(imap);
    CkMessageSetW_Dispose(messageSet);
    CkEmailBundleW_Dispose(bundle);
    CkEmailW_Dispose(email);

    }