Sample code for 30+ languages & platforms
Zig

IMAP Login using SecureString Credentials

See more IMAP Examples

Demonstrates the Chilkat Imap.LoginSecure method, which authenticates a connected session using SecureString credentials. The first argument is the login name and the second is the password. This is the secure-string counterpart of Login; the mechanism is chosen by the AuthMethod property.

Background: A SecureString keeps the credential encrypted in memory and helps avoid leaving plaintext copies lying around in the process, which is why the password should come from an interactive prompt, an environment variable, or a secrets vault — never a hard-coded literal. For XOAUTH2, pass the login name in the first argument and the raw access token (without a Bearer prefix) in the second.

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();

    // Demonstrates the Imap.LoginSecure method, which authenticates the session using SecureString
    // credentials.  The 1st argument is the login name and the 2nd is the password.

    const imap = try chilkat.Imap.init();
    defer imap.deinit();

    imap.setSsl(true);
    imap.setPort(993);

    imap.connect("imap.example.com") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // The password is not hard-coded in source.  Insert code here to obtain it from an
    // interactive prompt, environment variable, or a secrets vault.
    const password: [:0]const u8 = "";

    // Place the login name and password into SecureString objects.
    const secure_login = try chilkat.SecureString.init();
    defer secure_login.deinit();
    secure_login.append("user@example.com") catch {};
    const secure_password = try chilkat.SecureString.init();
    defer secure_password.deinit();
    secure_password.append(password) catch {};

    imap.loginSecure(secure_login, secure_password) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Logged in securely.\n", .{});

    imap.disconnect() catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };
}