Sample code for 30+ languages & platforms
Swift

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

Swift

func chilkatTest() {
    var success: Bool = false

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

    let mailman = CkoMailMan()!

    //  Configure the POP3 server connection.
    mailman.mailHost = "pop.example.com"
    mailman.mailPort = 995
    mailman.popSsl = true
    mailman.popUsername = "user@example.com"
    mailman.popPassword = "myPassword"

    //  Open and authenticate the SSH tunnel.
    success = mailman.sshOpenTunnel(sshHostname: "ssh.example.com", sshPort: 22)
    if success == false {
        print("\(mailman.lastErrorText!)")
        return
    }

    success = mailman.sshAuthenticatePw(sshLogin: "sshUser", sshPassword: "sshPassword")
    if success == false {
        print("\(mailman.lastErrorText!)")
        return
    }

    //  ... perform mail operations through the tunnel ...
    var count: Int = mailman.getMailboxCount().intValue
    if count < 0 {
        print("Failed to get the mailbox count.")
    }
    else {
        print("Mailbox count: \(count)")
    }

    //  Close the SSH tunnel when finished with it.
    success = mailman.sshCloseTunnel()
    if success == false {
        print("\(mailman.lastErrorText!)")
        return
    }

    print("SSH tunnel closed.")

}