Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Read iCloud Email Account using IMAP

See more IMAP Examples

Demonstrates how to set the IMAP settings for an iCloud email account and downloads the email from Inbox.

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

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

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

    // Connect to the iCloud IMAP Mail Server
    imap.setSsl(true);
    imap.setPort(993);
    imap.connect("imap.mail.me.com") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // The username is usually the name part of your iCloud email address
    // (for example, emilyparker, not emilyparker@icloud.com).
    imap.login("ICLOUD_USERNAME", "ICLOUD_PASSWORD") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Select an IMAP folder/mailbox
    imap.selectMailbox("Inbox") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Once the folder/mailbox is selected, the NumMessages property
    // will contain the number of emails in the mailbox.
    // Loop from 1 to NumMessages to fetch each email by sequence number.

    const email = try chilkat.Email.init();
    defer email.deinit();
    var i: i32 = 0;
    const n = imap.getNumMessages();
    const b_uid = false;
    i = 1;
    while (i <= n) : (i += 1) {
        // Download the email by sequence number.
        imap.fetchEmail(false, @as(u32, @intCast(i)), b_uid, email) catch {
            std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
            return;
        };

        std.debug.print("{d}: {s}\n", .{ i, try email.getFrom(alloc) });
        std.debug.print("    {s}\n", .{try email.getSubject(alloc)});
        std.debug.print("-\n", .{});
    }

    // Disconnect from the IMAP server.
    try imap.disconnect();

    std.debug.print("Success.\n", .{});

    // Sample output:

    //     1: iCloud <noreply@email.apple.com>
    //         Welcome to iCloud Mail.
    //     -
    //     2: "Chilkat Software" <support@chilkatsoft.com>
    //         This is a test
    //     -
    //     Success.
}