Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkSocket.pb"
IncludeFile "CkMailMan.pb"

Procedure ChilkatExample()

    success.i = 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.
    sshTunnel.i = CkSocket::ckCreate()
    If sshTunnel.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkSocket::ckSshOpenTunnel(sshTunnel,"ssh.example.com",22)
    If success = 0
        Debug CkSocket::ckLastErrorText(sshTunnel)
        CkSocket::ckDispose(sshTunnel)
        ProcedureReturn
    EndIf

    success = CkSocket::ckSshAuthenticatePw(sshTunnel,"sshUser","sshPassword")
    If success = 0
        Debug CkSocket::ckLastErrorText(sshTunnel)
        CkSocket::ckDispose(sshTunnel)
        ProcedureReturn
    EndIf

    mailman.i = CkMailMan::ckCreate()
    If mailman.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Configure the SMTP server connection.  These connections will travel through the tunnel.
    CkMailMan::setCkSmtpHost(mailman, "smtp.example.com")
    CkMailMan::setCkSmtpPort(mailman, 465)
    CkMailMan::setCkSmtpSsl(mailman, 1)
    CkMailMan::setCkSmtpUsername(mailman, "user@example.com")
    CkMailMan::setCkSmtpPassword(mailman, "myPassword")

    ;  Tell MailMan to route its connections through the existing tunnel.
    success = CkMailMan::ckUseSshTunnel(mailman,sshTunnel)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkSocket::ckDispose(sshTunnel)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

    ;  SMTP operations now travel through the shared SSH tunnel.
    success = CkMailMan::ckVerifySmtpLogin(mailman)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkSocket::ckDispose(sshTunnel)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

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


    CkSocket::ckDispose(sshTunnel)
    CkMailMan::ckDispose(mailman)


    ProcedureReturn
EndProcedure