Sample code for 30+ languages & platforms
Zig

Outlook -- Delete Email

See more Outlook Examples

Demonstrates how to delete email using the Microsoft Graph API.

Note: This example requires Chilkat v9.5.0.68 or greater.

This example applies to: Exchange Online | Office 365 | Hotmail.com | Live.com | MSN.com | Outlook.com | Passport.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();

    var success: bool = false;

    // 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:
    http.setAuthToken("MICROSOFT_GRAPH_ACCESS_TOKEN");

    // This example will search /Inbox for a message we want to delete.
    // First we need to get the folder ID for /Inbox.
    // Then we'll search for messages based on some criteria, and delete the matching messages.

    // Get the folder ID for /Inbox from the folder map created by this example
    const ht_folder_map = try chilkat.Hashtable.init();
    defer ht_folder_map.deinit();
    const sb_map = try chilkat.StringBuilder.init();
    defer sb_map.deinit();
    sb_map.loadFile("qa_data/outlook/folderMap.xml", "utf-8") catch {};
    ht_folder_map.addFromXmlSb(sb_map) catch {};

    // Get the ID for the "/Inbox" folder:
    const folder_id = ht_folder_map.lookupStr(alloc, "/Inbox") catch {
        std.debug.print("Folder ID not found\n", .{});
        return;
    };

    success = true;
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.setEmitCompact(false);

    // Search for emails in this folder with the phrase "Amazon SES" in the subject, and return only the id and subject.
    http.setUrlVar("folder_id", folder_id) catch {};
    http.setUrlVar("select", "id,subject") catch {};
    http.setUrlVar("filter", "contains(subject,'Amazon SES')") catch {};

    const sb_response = try chilkat.StringBuilder.init();
    defer sb_response.deinit();
    http.quickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}", sb_response) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    json.loadSb(sb_response) catch {};
    // Show the results..
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // Sample results:

    //     {
    //       "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA')/messages(id,subject)",
    //       "value": [
    //         {
    //           "@odata.etag": "W/\"CQAAABYAAADn68XtMop0TpsYJGpfKXY9AAA1jyl6\"",
    //           "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA5_vF7TKKdE6bGCRqXyl2PQAAADLHd_AAAAA=",
    //           "subject": "Amazon SES Address Verification Request in region US West (Oregon)"
    //         },
    //         {
    //           "@odata.etag": "W/\"CQAAABYAAADn68XtMop0TpsYJGpfKXY9AAA1jyl7\"",
    //           "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA5_vF7TKKdE6bGCRqXyl2PQAAADLHd_EAAAA=",
    //           "subject": "Amazon SES Address Verification Request in region US West (Oregon)"
    //         }
    //       ]
    //     }
    //

    // ------------
    // Proceed to delete each of the above emails...
    var resp: [:0]const u8 = "";
    var message_id: [:0]const u8 = "";
    var i: i32 = 0;
    const num_emails = json.sizeOfArray("value");
    while (i < num_emails) {
        json.setI(i);

        message_id = try json.stringOf(alloc, "value[i].id");
        http.setUrlVar("message_id", message_id) catch {};
        std.debug.print("Deleting {s}\n", .{message_id});
        resp = http.quickDeleteStr(alloc, "https://graph.microsoft.com/v1.0/me/messages/{$message_id}") catch {
            std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
            return;
        };

        // A 204 response indicates success.
        if (http.getLastStatus() == 204) {
            std.debug.print("Message deleted.\n", .{});
        } else {
            std.debug.print("Message not deleted.\n", .{});
            std.debug.print("{s}\n", .{resp});
        }

        i = i + 1;
    }

    // Sample output:

    // Deleting AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA5_vF7TKKdE6bGCRqXyl2PQAAADLHd_AAAAA=
    // Message deleted.
    // Deleting AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA5_vF7TKKdE6bGCRqXyl2PQAAADLHd_EAAAA=
    // Message deleted.
    //
}