Sample code for 30+ languages & platforms
Zig

OAuth2 for a GMail using a P12 Service Account Key

See more GMail REST API Examples

Demonstrates how to use GMail with OAuth2 for a service account within a Google Workspace Account where your email domain is custom (e.g., @yourcompany.com).

Note: This example does not work for Personal Google Accounts where the email domain is @gmail.com

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 http = try chilkat.Http.init();
    defer http.deinit();

    // --------------------------------------------------------------------------------
    // For a step-by-step guide for setting up your Google Workspace service account,
    // see Setup Google Workspace Account for Sending SMTP GMail from a Service Account
    // --------------------------------------------------------------------------------
    // Begin by loading your Google service account key (.p12)
    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    cert.loadPfxFile("c:/someDirectory/keys/chilkat25-cbd7b42afbd8.p12", "notasecret") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    // The ISS is your service account email address ending in gserviceaccount.com.
    const iss = "chilkatsvc@chilkat25.iam.gserviceaccount.com";

    // The scope is always the following string:
    const scope = "https://mail.google.com/";

    // The sub is your company email address
    const oauth_sub = "bob@yourcompany.com";

    // The access token is valid for this number of seconds.
    const num_sec = 3600;

    const access_token = http.gSvcOauthAccessToken(alloc, iss, scope, oauth_sub, num_sec, cert) catch "";
    if (!http.getLastMethodSuccess()) {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    } else {
        std.debug.print("access token: {s}\n", .{access_token});
    }

    // The access token allows us to send unlimited emails while it's valid. Once it expires, we must obtain and use a new one.

    // -----------------------------------------------------------------------
    const mailman = try chilkat.MailMan.init();
    defer mailman.deinit();

    // Set the properties for the GMail SMTP server:
    mailman.setSmtpHost("smtp.gmail.com");
    mailman.setSmtpPort(587);
    mailman.setStartTLS(true);

    mailman.setSmtpUsername("bob@yourcompany.com");
    mailman.setOAuth2AccessToken(access_token);

    // Create a new email object
    const email = try chilkat.Email.init();
    defer email.deinit();

    email.setSubject("This is a test");
    email.setBody("This is a test");
    email.setFrom("Bob <bob@yourcompany.com>");
    try email.addTo("Recipient", "recipient@example.com");
    // To add more recipients, call AddTo, AddCC, or AddBcc once per recipient.

    mailman.sendEmail(email) catch {
        std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
        return;
    };

    mailman.closeSmtpConnection() catch {
        std.debug.print("Connection to SMTP server not closed cleanly.\n", .{});
    };

    std.debug.print("Successfully sent email using Gmail with a service account key.\n", .{});
}