Sample code for 30+ languages & platforms
Lua

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

Lua
local chilkat = require("chilkat")

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

local mailman = chilkat.MailMan.new()

-- Configure the POP3 server connection.
mailman.mail_host = "pop.example.com"
mailman.mail_port = 995
mailman.pop_ssl = true
mailman.pop_username = "user@example.com"
mailman.pop_password = "myPassword"

-- Open and authenticate the SSH tunnel.
local success = mailman:ssh_open_tunnel("ssh.example.com", 22)
if not success then
    print(mailman.last_error_text)
    return
end

success = mailman:ssh_authenticate_pw("sshUser", "sshPassword")
if not success then
    print(mailman.last_error_text)
    return
end

-- ... perform mail operations through the tunnel ...
local count = mailman:get_mailbox_count()
if count < 0 then
    print("Failed to get the mailbox count.")
else
    print("Mailbox count: " .. count)
end

-- Close the SSH tunnel when finished with it.
success = mailman:ssh_close_tunnel()
if not success then
    print(mailman.last_error_text)
    return
end

print("SSH tunnel closed.")