Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
oleobject loo_Ssh
integer li_SshPort
string ls_SshPassword

li_Success = 0

//  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.

loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
    destroy loo_Imap
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Establish and authenticate the SSH connection separately.
loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")

li_SshPort = 22
li_Success = loo_Ssh.Connect("ssh.example.com",li_SshPort)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Imap
    destroy loo_Ssh
    return
end if

//  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.

li_Success = loo_Ssh.AuthenticatePw("sshUser",ls_SshPassword)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Imap
    destroy loo_Ssh
    return
end if

//  Use the SSH connection as the transport for IMAP.  One SSH connection can be shared by
//  several Chilkat objects because SSH supports multiple channels.
li_Success = loo_Imap.UseSsh(loo_Ssh)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Ssh
    return
end if

//  Now connect and log in to the IMAP server through the SSH connection.
loo_Imap.Ssl = 1
loo_Imap.Port = 993
li_Success = loo_Imap.Connect("imap.example.com")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Ssh
    return
end if

li_Success = loo_Imap.Login("user@example.com","myPassword")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Ssh
    return
end if

li_Success = loo_Imap.Disconnect()
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Ssh
    return
end if



destroy loo_Imap
destroy loo_Ssh