Sample code for 30+ languages & platforms
PureBasic

Close the SSH Tunnel

See more POP3 Examples

Demonstrates the Chilkat MailMan.SshCloseTunnel method, which closes the SSH tunnel used for SMTP or POP3 communication. This example opens and authenticates a tunnel, performs a mail operation through it, and then closes it.

Background: An SSH tunnel holds an open connection to the SSH server for as long as it is needed, so closing it releases both that connection and the resources on the SSH server. The natural pattern is open → authenticate → do mail work → close. It is the counterpart to SshOpenTunnel, and is separate from closing the mail connections themselves (CloseSmtpConnection / Pop3EndSession), which ride inside the tunnel.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkMailMan.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the MailMan.SshCloseTunnel method, which closes the SSH tunnel used for SMTP
    ;  or POP3 communication.

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

    ;  Configure the POP3 server connection.
    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 and authenticate the SSH tunnel.
    success = CkMailMan::ckSshOpenTunnel(mailman,"ssh.example.com",22)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

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

    ;  ... perform mail operations through the 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 SSH tunnel when finished with it.
    success = CkMailMan::ckSshCloseTunnel(mailman)
    If success = 0
        Debug CkMailMan::ckLastErrorText(mailman)
        CkMailMan::ckDispose(mailman)
        ProcedureReturn
    EndIf

    Debug "SSH tunnel closed."


    CkMailMan::ckDispose(mailman)


    ProcedureReturn
EndProcedure