Sample code for 30+ languages & platforms
Lua

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

Lua
local chilkat = require("chilkat")

-- 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.
local sshTunnel = chilkat.Socket.new()
local success = sshTunnel:ssh_open_tunnel("ssh.example.com", 22)
if not success then
    print(sshTunnel.last_error_text)
    return
end

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

local mailman = chilkat.MailMan.new()

-- Configure the SMTP server connection.  These connections will travel through the tunnel.
mailman.smtp_host = "smtp.example.com"
mailman.smtp_port = 465
mailman.smtp_ssl = true
mailman.smtp_username = "user@example.com"
mailman.smtp_password = "myPassword"

-- Tell MailMan to route its connections through the existing tunnel.
success = mailman:use_ssh_tunnel(sshTunnel)
if not success then
    print(mailman.last_error_text)
    return
end

-- SMTP operations now travel through the shared SSH tunnel.
success = mailman:verify_smtp_login()
if not success then
    print(mailman.last_error_text)
    return
end

print("Authenticated with the SMTP server through the SSH tunnel.")