Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSshTunnel
LOCAL oMailman

nSuccess := 0

//  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.
oSshTunnel := CreateObject("Chilkat.Socket")
nSuccess := oSshTunnel:SshOpenTunnel("ssh.example.com", 22)
IF (nSuccess == 0)
    ? oSshTunnel:LastErrorText
    oSshTunnel:destroy()
    RETURN
ENDIF

nSuccess := oSshTunnel:SshAuthenticatePw("sshUser", "sshPassword")
IF (nSuccess == 0)
    ? oSshTunnel:LastErrorText
    oSshTunnel:destroy()
    RETURN
ENDIF

oMailman := CreateObject("Chilkat.MailMan")

//  Configure the SMTP server connection.  These connections will travel through the tunnel.
oMailman:SmtpHost := "smtp.example.com"
oMailman:SmtpPort := 465
oMailman:SmtpSsl := 1
oMailman:SmtpUsername := "user@example.com"
oMailman:SmtpPassword := "myPassword"

//  Tell MailMan to route its connections through the existing tunnel.
nSuccess := oMailman:UseSshTunnel(oSshTunnel)
IF (nSuccess == 0)
    ? oMailman:LastErrorText
    oSshTunnel:destroy()
    oMailman:destroy()
    RETURN
ENDIF

//  SMTP operations now travel through the shared SSH tunnel.
nSuccess := oMailman:VerifySmtpLogin()
IF (nSuccess == 0)
    ? oMailman:LastErrorText
    oSshTunnel:destroy()
    oMailman:destroy()
    RETURN
ENDIF

? "Authenticated with the SMTP server through the SSH tunnel."

oSshTunnel:destroy()
oMailman:destroy()