Zig
Zig
SMTP XOAUTH2 Authentication
See more SMTP Examples
Demonstrates how to authenticate using an OAuth2 access token. This example assumes you have already obtained the access token and now wish to use it in an SMTP session.Chilkat Zig Downloads
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 oauth2_access_token = "some_previously_obtained_token";
const mailman = try chilkat.MailMan.init();
defer mailman.deinit();
// Set the properties for the SMTP server, whatever they may be..
mailman.setSmtpHost("smtp.example.com");
mailman.setSmtpPort(587);
mailman.setStartTLS(true);
mailman.setSmtpUsername("myLogin");
mailman.setOAuth2AccessToken(oauth2_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("Joe Example <joe@example.com>");
email.addTo("Chilkat Admin", "admin@chilkatsoft.com") catch {};
// Connect to the server indicated by the SmtpHost property
mailman.smtpConnect() catch {
std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
return;
};
// Authenticate using XOAUTH2 (using the OAuth2AccessToken)
mailman.smtpAuthenticate() catch {
std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
return;
};
mailman.sendEmail(email) catch {
std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
return;
};
std.debug.print("Email sent.\n", .{});
mailman.closeSmtpConnection() catch {};
}