Zig
Zig
Route IMAP Through an Existing Ssh Connection
See more IMAP Examples
Demonstrates the Chilkat Imap.UseSsh method, which uses an already connected and authenticated Ssh object as the transport for IMAP connections. The only argument is the Ssh object. Call it before Connect. This example establishes the SSH connection separately, then routes IMAP through it.
Background: SSH supports multiple logical channels over one connection, so a single authenticated
Ssh object can be shared — IMAP, SMTP, and other Chilkat objects can all tunnel through the same SSH session. This differs from SshOpenTunnel, where the Imap object owns and manages the tunnel itself; with UseSsh you manage the Ssh object's lifetime.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// Demonstrates the Imap.UseSsh method, which uses an already connected and authenticated Ssh
// object as the transport for IMAP connections. The only argument is the Ssh object. Call it
// before Connect.
const imap = try chilkat.Imap.init();
defer imap.deinit();
// Establish and authenticate the SSH connection separately.
const ssh = try chilkat.Ssh.init();
defer ssh.deinit();
const ssh_port = 22;
ssh.connect("ssh.example.com", ssh_port) catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
// The SSH password is not hard-coded in source. Insert code here to obtain it from an
// interactive prompt, environment variable, or a secrets vault.
const ssh_password: [:0]const u8 = "";
ssh.authenticatePw("sshUser", ssh_password) catch {
std.debug.print("{s}\n", .{try ssh.getLastErrorText(alloc)});
return;
};
// Use the SSH connection as the transport for IMAP. One SSH connection can be shared by
// several Chilkat objects because SSH supports multiple channels.
imap.useSsh(ssh) catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
// Now connect and log in to the IMAP server through the SSH connection.
imap.setSsl(true);
imap.setPort(993);
imap.connect("imap.example.com") catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
imap.login("user@example.com", "myPassword") catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
imap.disconnect() catch {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
};
}