Zig
Zig
Outlook Send Email using MIME Format
See more Outlook Examples
This example sends an email using MIME format via the Microsoft Graph Outlook REST API.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 http = try chilkat.Http.init();
defer http.deinit();
// Use your previously obtained access token here:
// See the following examples for getting an access token:
// Get Microsoft Graph OAuth2 Access Token (Azure AD v2.0 Endpoint).
// Get Microsoft Graph OAuth2 Access Token (Azure AD Endpoint).
// Refresh Access Token (Azure AD v2.0 Endpoint).
// Refresh Access Token (Azure AD Endpoint).
const json_token = try chilkat.JsonObject.init();
defer json_token.deinit();
json_token.loadFile("qa_data/tokens/microsoftGraph.json") catch {
std.debug.print("{s}\n", .{try json_token.getLastErrorText(alloc)});
return;
};
http.setAuthToken(try json_token.stringOf(alloc, "access_token"));
// To send email, we'll POST to the following endpoint:
//
// POST /users/{id | userPrincipalName}/sendMail
//
// (The special keyword "me" may be used in place of a principal name.)
//
// The body of the POST request will contain the MIME source of the email in base64 format.
// Create a new email object
const email = try chilkat.Email.init();
defer email.deinit();
email.setSubject("Test Outlook API to Send HTML Email with Attachments");
email.setFrom("Mary <mary@somewhere.com>");
email.addTo("Joe", "joe@example.com") catch {};
// Add a plain-text alternative body, which will likely never be seen.
// (It is shown if the receiving email client is incapable of displaying HTML email.)
email.addPlainTextAlternativeBody("This is a plain-text alternative body...") catch {};
// Our HTML will include an image, so add the related image here.
const content_id_starfish = email.addRelatedFile(alloc, "qa_data/jpg/starfish.jpg") catch {
std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
return;
};
// The src attribute for the image tag is set to the contentIdStarfish:
const sb_html = try chilkat.StringBuilder.init();
defer sb_html.deinit();
sb_html.append("<html><body><p>This is an HTML email with an embedded image.</p>") catch {};
sb_html.append("<p><img src=\"cid:CONTENT_ID_STARFISH\" /></p></body></html>") catch {};
_ = sb_html.replace("CONTENT_ID_STARFISH", content_id_starfish);
email.addHtmlAlternativeBody(try sb_html.getAsString(alloc)) catch {};
// Finally, add some attachments to the email.
// Add a file attachment.
email.addFileAttachment2("qa_data/pdf/fishing.pdf", "application/pdf") catch {
std.debug.print("{s}\n", .{try email.getLastErrorText(alloc)});
return;
};
// Add an attachment where the content is contained in a string.
const content = "This is the content of the 2nd attached file.";
email.addStringAttachment("someText.txt", content) catch {};
// Get the email as multi-line base64..
const bd_mime = try chilkat.BinData.init();
defer bd_mime.deinit();
email.getMimeBd(bd_mime) catch {};
// Now get it as multi-line base64
const sb_base64 = try chilkat.StringBuilder.init();
defer sb_base64.deinit();
bd_mime.getEncodedSb("base64_mime", sb_base64) catch {};
// Send the HTTP POST (i.e. send the email)
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpSb("POST", "https://graph.microsoft.com/v1.0/me/sendMail", sb_base64, "utf-8", "text/plain", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
// The send succeeded if the response status code = 202.
// In the success case, there is no response body. (We just get the response code to know that it succeeded.)
if (resp.getStatusCode() != 202) {
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.load(try resp.getBodyStr(alloc)) catch {};
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
std.debug.print("Failed, response status code = {d}\n", .{resp.getStatusCode()});
} else {
std.debug.print("Outlook Mail Sent.\n", .{});
}
}