Zig
Zig
Send an Email via SMTP
See more SMTP Examples
Demonstrates the Chilkat MailMan.SendEmail method, which sends a single Email object through the configured SMTP server. This example configures the SMTP connection, builds a message, and sends it.
Background: SMTP (Simple Mail Transfer Protocol) is the protocol for sending mail. The typical flow is: connect to the server, authenticate, then hand over the message with
MAIL FROM, RCPT TO, and DATA. SendEmail wraps all of that: you build an Email with subject, recipients, and body, and Chilkat renders it to MIME and delivers it. Modern submission commonly uses implicit TLS on port 465 (SmtpSsl = true).Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// Demonstrates the MailMan.SendEmail method, which sends a single Email object through the
// configured SMTP server.
const mailman = try chilkat.MailMan.init();
defer mailman.deinit();
// Configure the SMTP server connection.
mailman.setSmtpHost("smtp.example.com");
mailman.setSmtpPort(465);
mailman.setSmtpSsl(true);
mailman.setSmtpUsername("user@example.com");
mailman.setSmtpPassword("myPassword");
// Build the email to send.
const email = try chilkat.Email.init();
defer email.deinit();
email.setSubject("Test email from Chilkat");
email.setFrom("alice@example.com");
email.addTo("Bob", "bob@example.com") catch {};
email.setBody("Hello, this is a test message.");
// Send the email.
mailman.sendEmail(email) catch {
std.debug.print("{s}\n", .{try mailman.getLastErrorText(alloc)});
return;
};
std.debug.print("Email sent.\n", .{});
// Note: Explicitly connecting/authenticating is optional. Chilkat MailMan automatically
// connects and authenticates -- using the property settings above -- whenever a server
// operation requires it. Calling the explicit connect/authenticate methods can still be
// helpful to determine whether a failure occurs while connecting or while authenticating.
}