Unicode C
Unicode 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 Unicode C Downloads
#include <C_CkImapW.h>
#include <C_CkSocketW.h>
void ChilkatSample(void)
{
BOOL success;
HCkImapW imap;
HCkSocketW sshTunnel;
int sshPort;
const wchar_t *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 = CkImapW_Create();
// Open and authenticate an SSH tunnel on a Socket object. This tunnel can be shared with
// other Chilkat objects.
sshTunnel = CkSocketW_Create();
sshPort = 22;
success = CkSocketW_SshOpenTunnel(sshTunnel,L"ssh.example.com",sshPort);
if (success == FALSE) {
wprintf(L"%s\n",CkSocketW_lastErrorText(sshTunnel));
CkImapW_Dispose(imap);
CkSocketW_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 = CkSocketW_SshAuthenticatePw(sshTunnel,L"sshUser",sshPassword);
if (success == FALSE) {
wprintf(L"%s\n",CkSocketW_lastErrorText(sshTunnel));
CkImapW_Dispose(imap);
CkSocketW_Dispose(sshTunnel);
return;
}
// Tell the Imap object to use the existing tunnel.
success = CkImapW_UseSshTunnel(imap,sshTunnel);
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
CkSocketW_Dispose(sshTunnel);
return;
}
// Now connect and log in to the IMAP server through the tunnel.
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);
CkSocketW_Dispose(sshTunnel);
return;
}
success = CkImapW_Login(imap,L"user@example.com",L"myPassword");
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
CkSocketW_Dispose(sshTunnel);
return;
}
success = CkImapW_Disconnect(imap);
if (success == FALSE) {
wprintf(L"%s\n",CkImapW_lastErrorText(imap));
CkImapW_Dispose(imap);
CkSocketW_Dispose(sshTunnel);
return;
}
CkImapW_Dispose(imap);
CkSocketW_Dispose(sshTunnel);
}