Sample code for 30+ languages & platforms
C

Share an Existing SSH Tunnel (Socket) with IMAP

See more IMAP Examples

Demonstrates the Chilkat Imap.UseSshTunnel method, which uses an existing SSH tunnel, represented by a Socket object, for IMAP connections. The only argument is the Socket. Call it before Connect. This example opens and authenticates the tunnel on a Socket, then routes IMAP through it.

Background: A Socket object can open and hold an SSH tunnel that several Chilkat objects share. This is the approach when the tunnel is managed independently of any one protocol object — for example a long-lived tunnel reused across IMAP, POP3, and SMTP sessions — whereas SshOpenTunnel ties the tunnel's lifetime to the Imap object.

Chilkat C Downloads

C
#include <C_CkImap.h>
#include <C_CkSocket.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkImap imap;
    HCkSocket sshTunnel;
    int sshPort;
    const char *sshPassword;

    success = FALSE;

    //  Demonstrates the Imap.UseSshTunnel method, which uses an existing SSH tunnel (represented by
    //  a Socket) for IMAP connections.  The only argument is the Socket.  Call it before Connect.

    imap = CkImap_Create();

    //  Open and authenticate an SSH tunnel on a Socket object.  This tunnel can be shared with
    //  other Chilkat objects.
    sshTunnel = CkSocket_Create();
    sshPort = 22;
    success = CkSocket_SshOpenTunnel(sshTunnel,"ssh.example.com",sshPort);
    if (success == FALSE) {
        printf("%s\n",CkSocket_lastErrorText(sshTunnel));
        CkImap_Dispose(imap);
        CkSocket_Dispose(sshTunnel);
        return;
    }

    //  The SSH password is not hard-coded in source.  Insert code here to obtain it from an
    //  interactive prompt, environment variable, or a secrets vault.

    success = CkSocket_SshAuthenticatePw(sshTunnel,"sshUser",sshPassword);
    if (success == FALSE) {
        printf("%s\n",CkSocket_lastErrorText(sshTunnel));
        CkImap_Dispose(imap);
        CkSocket_Dispose(sshTunnel);
        return;
    }

    //  Tell the Imap object to use the existing tunnel.
    success = CkImap_UseSshTunnel(imap,sshTunnel);
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        CkSocket_Dispose(sshTunnel);
        return;
    }

    //  Now connect and log in to the IMAP server through the tunnel.
    CkImap_putSsl(imap,TRUE);
    CkImap_putPort(imap,993);
    success = CkImap_Connect(imap,"imap.example.com");
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        CkSocket_Dispose(sshTunnel);
        return;
    }

    success = CkImap_Login(imap,"user@example.com","myPassword");
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        CkSocket_Dispose(sshTunnel);
        return;
    }

    success = CkImap_Disconnect(imap);
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        CkSocket_Dispose(sshTunnel);
        return;
    }



    CkImap_Dispose(imap);
    CkSocket_Dispose(sshTunnel);

    }