Sample code for 30+ languages & platforms
Unicode C

Use an Existing Socket SSH Tunnel

See more SMTP Examples

Demonstrates the Chilkat MailMan.UseSshTunnel method, which configures MailMan to use an existing SSH tunnel provided by a Socket object for its SMTP and POP3 connections. This example opens and authenticates a tunnel on a Socket, hands it to MailMan, and then verifies an SMTP login through it.

Background: Sharing one already-established SSH tunnel across objects avoids opening (and authenticating) a separate SSH connection for every component that needs to reach the remote network. Here the tunnel lives on a Socket object, which MailMan then routes its SMTP/POP3 traffic through. It is the Socket-based sibling of UseSsh, which shares an Ssh object instead.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkSocketW.h>
#include <C_CkMailManW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSocketW sshTunnel;
    HCkMailManW mailman;

    success = FALSE;

    //  Demonstrates the MailMan.UseSshTunnel method, which configures MailMan to use an existing
    //  SSH tunnel provided by a Socket object for its SMTP and POP3 connections.

    //  Open and authenticate the SSH tunnel using a Socket object.
    sshTunnel = CkSocketW_Create();
    success = CkSocketW_SshOpenTunnel(sshTunnel,L"ssh.example.com",22);
    if (success == FALSE) {
        wprintf(L"%s\n",CkSocketW_lastErrorText(sshTunnel));
        CkSocketW_Dispose(sshTunnel);
        return;
    }

    success = CkSocketW_SshAuthenticatePw(sshTunnel,L"sshUser",L"sshPassword");
    if (success == FALSE) {
        wprintf(L"%s\n",CkSocketW_lastErrorText(sshTunnel));
        CkSocketW_Dispose(sshTunnel);
        return;
    }

    mailman = CkMailManW_Create();

    //  Configure the SMTP server connection.  These connections will travel through the tunnel.
    CkMailManW_putSmtpHost(mailman,L"smtp.example.com");
    CkMailManW_putSmtpPort(mailman,465);
    CkMailManW_putSmtpSsl(mailman,TRUE);
    CkMailManW_putSmtpUsername(mailman,L"user@example.com");
    CkMailManW_putSmtpPassword(mailman,L"myPassword");

    //  Tell MailMan to route its connections through the existing tunnel.
    success = CkMailManW_UseSshTunnel(mailman,sshTunnel);
    if (success == FALSE) {
        wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
        CkSocketW_Dispose(sshTunnel);
        CkMailManW_Dispose(mailman);
        return;
    }

    //  SMTP operations now travel through the shared SSH tunnel.
    success = CkMailManW_VerifySmtpLogin(mailman);
    if (success == FALSE) {
        wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
        CkSocketW_Dispose(sshTunnel);
        CkMailManW_Dispose(mailman);
        return;
    }

    wprintf(L"Authenticated with the SMTP server through the SSH tunnel.\n");


    CkSocketW_Dispose(sshTunnel);
    CkMailManW_Dispose(mailman);

    }