PureBasic
PureBasic
Open an SSH Tunnel for POP3
See more POP3 Examples
Demonstrates the Chilkat MailMan.SshOpenTunnel method, which connects to an SSH server and opens an SSH tunnel for MailMan's SMTP or POP3 connections. The first argument is the SSH server's hostname or IP address and the second is the SSH port. This example opens a tunnel, authenticates, performs a POP3 operation through it, and closes the tunnel.
Background: An SSH tunnel (port forwarding) wraps another protocol's traffic inside an encrypted SSH connection. Here, MailMan's POP3 or SMTP session is carried through the SSH server rather than connecting directly. This is useful when a mail server is only reachable from inside a network you can reach via SSH, or when policy requires all traffic to traverse a bastion host. Opening the tunnel is step one; you must then authenticate to the SSH server before the tunnel carries traffic.
Chilkat PureBasic Downloads
IncludeFile "CkMailMan.pb"
Procedure ChilkatExample()
success.i = 0
; Demonstrates the MailMan.SshOpenTunnel method, which connects to an SSH server and opens
; an SSH tunnel for MailMan's SMTP or POP3 connections. The 1st argument is the SSH server
; hostname or IP address, and the 2nd is the SSH port.
mailman.i = CkMailMan::ckCreate()
If mailman.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Configure the POP3 server connection. These connections will travel through the tunnel.
CkMailMan::setCkMailHost(mailman, "pop.example.com")
CkMailMan::setCkMailPort(mailman, 995)
CkMailMan::setCkPopSsl(mailman, 1)
CkMailMan::setCkPopUsername(mailman, "user@example.com")
CkMailMan::setCkPopPassword(mailman, "myPassword")
; Open the SSH tunnel (port 22 is the standard SSH port).
success = CkMailMan::ckSshOpenTunnel(mailman,"ssh.example.com",22)
If success = 0
Debug CkMailMan::ckLastErrorText(mailman)
CkMailMan::ckDispose(mailman)
ProcedureReturn
EndIf
; Authenticate with the SSH server.
success = CkMailMan::ckSshAuthenticatePw(mailman,"sshUser","sshPassword")
If success = 0
Debug CkMailMan::ckLastErrorText(mailman)
CkMailMan::ckDispose(mailman)
ProcedureReturn
EndIf
; POP3 operations now travel through the SSH tunnel.
count.i = CkMailMan::ckGetMailboxCount(mailman)
If count < 0
Debug "Failed to get the mailbox count."
Else
Debug "Mailbox count: " + Str(count)
EndIf
; Close the tunnel when finished.
success = CkMailMan::ckSshCloseTunnel(mailman)
If success = 0
Debug CkMailMan::ckLastErrorText(mailman)
CkMailMan::ckDispose(mailman)
ProcedureReturn
EndIf
CkMailMan::ckDispose(mailman)
ProcedureReturn
EndProcedure