Sample code for 30+ languages & platforms
Zig

Check Whether the IMAP Session Is Authenticated

See more IMAP Examples

Demonstrates the Chilkat Imap.IsLoggedIn method, which indicates whether this object is in an authenticated IMAP session. It reports the last known state and does not send a command to the server. This example connects, logs in, and checks the state.

Background: An IMAP session has two levels: connected (the TCP/TLS link is up) and authenticated (login succeeded). IsLoggedIn reports the second — useful when reusing a long-lived Imap object to decide whether you still need to log in before performing mailbox operations. Like IsConnected, it reflects cached state and doesn't contact the server.

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.IsLoggedIn method, which indicates whether this object is in an
    // authenticated IMAP session.  It reports the last known state and does not send a command
    // to the server.

    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;
    };

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

    // Check whether the session is authenticated.
    const logged_in = imap.isLoggedIn();
    if (logged_in) {
        std.debug.print("The session is authenticated.\n", .{});
    } else {
        std.debug.print("The session is not authenticated.\n", .{});
    }

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