Sample code for 30+ languages & platforms
Unicode C

Connect to an IMAP Server

See more IMAP Examples

Demonstrates the Chilkat Imap.Connect method, which establishes a TCP connection to the IMAP server but does not authenticate. The argument may be a hostname, IPv4, or IPv6 address; configure Ssl and Port first. This example connects with implicit TLS, then logs in and disconnects.

Background: IMAP (Internet Message Access Protocol) is the protocol for accessing mail that stays on the server — unlike POP3, which typically downloads and removes it. That server-side model is what lets multiple devices see the same mailboxes, folders, and read/unread state. A session starts by connecting (usually implicit TLS on port 993), then authenticating — Connect performs only the first step, so it is always followed by Login.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkImapW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imap;

    success = FALSE;

    //  Demonstrates the Imap.Connect method, which establishes a TCP connection to the IMAP
    //  server but does not authenticate.  Call Login after connecting to authenticate.

    imap = CkImapW_Create();

    //  Use implicit TLS on the standard IMAPS port.
    CkImapW_putSsl(imap,TRUE);
    CkImapW_putPort(imap,993);

    //  Establish the connection to the IMAP server (no authentication yet).
    success = CkImapW_Connect(imap,L"imap.example.com");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    wprintf(L"Connected to the IMAP server.\n");

    //  Authenticate the session.
    success = CkImapW_Login(imap,L"user@example.com",L"myPassword");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    //  Disconnect when finished.
    success = CkImapW_Disconnect(imap);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }



    CkImapW_Dispose(imap);

    }