Sample code for 30+ languages & platforms
PureBasic

Use an Existing Ssh Object as the Tunnel

See more POP3 Examples

Demonstrates the Chilkat MailMan.UseSsh method, which configures MailMan to use an existing SSH tunnel provided by an Ssh object for its SMTP and POP3 connections. This example connects and authenticates an Ssh object separately, hands it to MailMan, and then performs a POP3 operation through it.

Background: Rather than having MailMan open its own tunnel (SshOpenTunnel), you can manage the SSH connection yourself and share it. That is valuable when several Chilkat objects must reach the same remote network — they all ride one authenticated SSH connection instead of each opening its own — and it enables advanced setups such as multi-hop SSH, where the Ssh object is already chained through intermediate servers.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSsh.pb"
IncludeFile "CkMailMan.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the MailMan.UseSsh method, which configures MailMan to use an existing SSH
    ;  tunnel provided by an Ssh object for its SMTP and POP3 connections.

    ;  Establish the SSH connection separately, using an Ssh object.
    ssh.i = CkSsh::ckCreate()
    If ssh.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkSsh::ckConnect(ssh,"ssh.example.com",22)
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    success = CkSsh::ckAuthenticatePw(ssh,"sshUser","sshPassword")
    If success = 0
        Debug CkSsh::ckLastErrorText(ssh)
        CkSsh::ckDispose(ssh)
        ProcedureReturn
    EndIf

    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")

    ;  Tell MailMan to route its connections through the already-connected Ssh object.
    success = CkMailMan::ckUseSsh(mailman,ssh)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkSsh::ckDispose(ssh)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

    ;  POP3 operations now travel through the shared SSH connection.
    count.i = CkMailMan::ckGetMailboxCount(mailman)
    If count < 0
        Debug "Failed to get the mailbox count."
    Else
        Debug "Mailbox count: " + Str(count)
    EndIf

    CkSsh::ckDisconnect(ssh)


    CkSsh::ckDispose(ssh)
    CkMailMan::ckDispose(mailman)


    ProcedureReturn
EndProcedure