Zig
Zig
Authenticate an SSH Tunnel with a Password
See more SMTP Examples
Demonstrates the Chilkat MailMan.SshAuthenticatePw method, which authenticates with the SSH server using an SSH username and password after an SSH tunnel has been opened. The first argument is the SSH account name and the second is the corresponding password. This example opens a tunnel, authenticates, and sends SMTP traffic through it.
Background: Opening an SSH tunnel and authenticating to the SSH server are two separate steps, and note the SSH credentials are distinct from the mail account credentials — the SSH login gets you through the tunnel, while
SmtpUsername/SmtpPassword authenticate to the mail server at the far end. Password authentication is the simplest option; public-key authentication (SshAuthenticatePk) is generally preferred for automated systems.Chilkat Zig Downloads
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.SshAuthenticatePw method, which authenticates with the SSH server
// using an SSH username and password after an SSH tunnel has been opened. The 1st argument
// is the SSH account name and the 2nd is the corresponding password.
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");
// Open the SSH tunnel first.
success = if (mailman.sshOpenTunnel("ssh.example.com", 22)) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
return;
}
// Authenticate with the SSH server using a username and password.
success = if (mailman.sshAuthenticatePw("sshUser", "sshPassword")) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
return;
}
std.debug.print("SSH tunnel authenticated.\n", .{});
// SMTP operations now travel through the SSH tunnel.
success = mailman.verifySmtpLogin();
if (!success) {
std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
return;
}
mailman.sshCloseTunnel() catch {
std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
return;
};
}