Sample code for 30+ languages & platforms
Rust

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

Rust
let mut success = false;

// 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.
let ssh_tunnel = chilkat::Socket::new();
if ssh_tunnel.ssh_open_tunnel("ssh.example.com", 22).is_err() {
    println!("{}", ssh_tunnel.last_error_text());
    return;
}

if ssh_tunnel.ssh_authenticate_pw("sshUser", "sshPassword").is_err() {
    println!("{}", ssh_tunnel.last_error_text());
    return;
}

let mailman = chilkat::MailMan::new();

// Configure the SMTP server connection.  These connections will travel through the tunnel.
mailman.set_smtp_host("smtp.example.com");
mailman.set_smtp_port(465);
mailman.set_smtp_ssl(true);
mailman.set_smtp_username("user@example.com");
mailman.set_smtp_password("myPassword");

// Tell MailMan to route its connections through the existing tunnel.
if mailman.use_ssh_tunnel(&ssh_tunnel).is_err() {
    println!("{}", mailman.last_error_text());
    return;
}

// SMTP operations now travel through the shared SSH tunnel.
success = mailman.verify_smtp_login();
if !success {
    println!("{}", mailman.last_error_text());
    return;
}

println!("Authenticated with the SMTP server through the SSH tunnel.");