Sample code for 30+ languages & platforms
Unicode C

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 Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkHashtableW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkHashtableW htFolderMap;
    HCkStringBuilderW sbMap;
    const wchar_t *folderId;
    HCkJsonObjectW json;
    HCkStringBuilderW sbResponse;
    const wchar_t *resp;
    const wchar_t *messageId;
    int i;
    int numEmails;

    success = FALSE;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    http = CkHttpW_Create();

    // Use your previously obtained access token here:
    CkHttpW_putAuthToken(http,L"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 
    htFolderMap = CkHashtableW_Create();
    sbMap = CkStringBuilderW_Create();
    CkStringBuilderW_LoadFile(sbMap,L"qa_data/outlook/folderMap.xml",L"utf-8");
    CkHashtableW_AddFromXmlSb(htFolderMap,sbMap);

    // Get the ID for the "/Inbox" folder:
    folderId = CkHashtableW_lookupStr(htFolderMap,L"/Inbox");
    if (CkHashtableW_getLastMethodSuccess(htFolderMap) != TRUE) {
        wprintf(L"Folder ID not found\n");
        CkHttpW_Dispose(http);
        CkHashtableW_Dispose(htFolderMap);
        CkStringBuilderW_Dispose(sbMap);
        return;
    }

    success = TRUE;
    json = CkJsonObjectW_Create();
    CkJsonObjectW_putEmitCompact(json,FALSE);

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

    sbResponse = CkStringBuilderW_Create();
    success = CkHttpW_QuickGetSb(http,L"https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse);
    if (success != TRUE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkHashtableW_Dispose(htFolderMap);
        CkStringBuilderW_Dispose(sbMap);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbResponse);
        return;
    }

    CkJsonObjectW_LoadSb(json,sbResponse);
    // Show the results..
    wprintf(L"%s\n",CkJsonObjectW_emit(json));

    // 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...

    i = 0;
    numEmails = CkJsonObjectW_SizeOfArray(json,L"value");
    while (i < numEmails) {
        CkJsonObjectW_putI(json,i);

        messageId = CkJsonObjectW_stringOf(json,L"value[i].id");
        CkHttpW_SetUrlVar(http,L"message_id",messageId);
        wprintf(L"Deleting %s\n",messageId);
        resp = CkHttpW_quickDeleteStr(http,L"https://graph.microsoft.com/v1.0/me/messages/{$message_id}");
        if (CkHttpW_getLastMethodSuccess(http) != TRUE) {
            wprintf(L"%s\n",CkHttpW_lastErrorText(http));
            CkHttpW_Dispose(http);
            CkHashtableW_Dispose(htFolderMap);
            CkStringBuilderW_Dispose(sbMap);
            CkJsonObjectW_Dispose(json);
            CkStringBuilderW_Dispose(sbResponse);
            return;
        }

        // A 204 response indicates success.
        if (CkHttpW_getLastStatus(http) == 204) {
            wprintf(L"Message deleted.\n");
        }
        else {
            wprintf(L"Message not deleted.\n");
            wprintf(L"%s\n",resp);
        }

        i = i + 1;
    }

    // Sample output:

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


    CkHttpW_Dispose(http);
    CkHashtableW_Dispose(htFolderMap);
    CkStringBuilderW_Dispose(sbMap);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbResponse);

    }