Sample code for 30+ languages & platforms
Zig

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

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    var success: bool = 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.
    const ssh_tunnel = try chilkat.Socket.init();
    defer ssh_tunnel.deinit();
    success = if (ssh_tunnel.sshOpenTunnel("ssh.example.com", 22)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try ssh_tunnel.getLastErrorText(alloc)});
        return;
    }

    success = if (ssh_tunnel.sshAuthenticatePw("sshUser", "sshPassword")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try ssh_tunnel.getLastErrorText(alloc)});
        return;
    }

    const mailman = try chilkat.MailMan.init();
    defer mailman.deinit();

    // Configure the SMTP server connection.  These connections will travel through the tunnel.
    mailman.setSmtpHost("smtp.example.com");
    mailman.setSmtpPort(465);
    mailman.setSmtpSsl(true);
    mailman.setSmtpUsername("user@example.com");
    mailman.setSmtpPassword("myPassword");

    // Tell MailMan to route its connections through the existing tunnel.
    success = if (mailman.useSshTunnel(ssh_tunnel)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
        return;
    }

    // SMTP operations now travel through the shared SSH tunnel.
    success = mailman.verifySmtpLogin();
    if (!success) {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
        return;
    }

    std.debug.print("Authenticated with the SMTP server through the SSH tunnel.\n", .{});
}