Unicode C++
Unicode C++
Route IMAP Through an Existing Ssh Connection
See more IMAP Examples
Demonstrates the Chilkat Imap.UseSsh method, which uses an already connected and authenticated Ssh object as the transport for IMAP connections. The only argument is the Ssh object. Call it before Connect. This example establishes the SSH connection separately, then routes IMAP through it.
Background: SSH supports multiple logical channels over one connection, so a single authenticated
Ssh object can be shared — IMAP, SMTP, and other Chilkat objects can all tunnel through the same SSH session. This differs from SshOpenTunnel, where the Imap object owns and manages the tunnel itself; with UseSsh you manage the Ssh object's lifetime.Chilkat Unicode C++ Downloads
#include <CkImapW.h>
#include <CkSshW.h>
void ChilkatSample(void)
{
bool success = false;
// Demonstrates the Imap.UseSsh method, which uses an already connected and authenticated Ssh
// object as the transport for IMAP connections. The only argument is the Ssh object. Call it
// before Connect.
CkImapW imap;
// Establish and authenticate the SSH connection separately.
CkSshW ssh;
int sshPort = 22;
success = ssh.Connect(L"ssh.example.com",sshPort);
if (success == false) {
wprintf(L"%s\n",ssh.lastErrorText());
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.
const wchar_t *sshPassword = 0;
success = ssh.AuthenticatePw(L"sshUser",sshPassword);
if (success == false) {
wprintf(L"%s\n",ssh.lastErrorText());
return;
}
// Use the SSH connection as the transport for IMAP. One SSH connection can be shared by
// several Chilkat objects because SSH supports multiple channels.
success = imap.UseSsh(ssh);
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
// Now connect and log in to the IMAP server through the SSH connection.
imap.put_Ssl(true);
imap.put_Port(993);
success = imap.Connect(L"imap.example.com");
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
success = imap.Login(L"user@example.com",L"myPassword");
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
success = imap.Disconnect();
if (success == false) {
wprintf(L"%s\n",imap.lastErrorText());
return;
}
}