Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

C++ Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(C++) Outlook -- Search Multiple Folders

I don't yet see how it's possible to do a recursive search of Outlook folders using the Microsoft Graph API. My best guess is to somehow use OData v4.0's $expand query option, but the hierarchical structure of the "mailFolder" and "messages" Microsoft Graph resources don't quite fit. Suggestions are welcome and can be sent to support@chilkatsoft.com.

This example will iterate over a list of folder previously obtained by a recursive traversal of the Outlook mail folders. (See the link in the example code below.)

A separate search is performed on each desired folder, and the results are combined into a single result set.

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 C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Solaris C/C++

MinGW C/C++

#include <CkHttp.h>
#include <CkXml.h>
#include <CkJsonObject.h>
#include <CkStringBuilder.h>

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

    CkHttp http;

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

    http.put_AuthToken("MICROSOFT_GRAPH_ACCESS_TOKEN");

    // This example will iterate over the folders previously discovered by recursively traversing the Outlook folders
    // as shown in this example:  Outlook Recursive Folder Traversal)

    // The XML map produced by the recursive traversal looks like this:

    // 	<?xml version="1.0" encoding="utf-8"?>
    // 	<hashtable>
    // 	<e><k>/Sent Items</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEJAAAA</v></e>
    // 	<e><k>/Inbox</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA</v></e>
    // 	<e><k>/Junk Email</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEiAAAA</v></e>
    // 	<e><k>/Inbox/xyz</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwEAAAA=</v></e>
    // 	<e><k>/Inbox/abc/subFolderA/a</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwIAAAA=</v></e>
    // 	<e><k>/Inbox/abc</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huv8AAAA=</v></e>
    // 	<e><k>/Outbox</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgELAAAA</v></e>
    // 	<e><k>/Inbox/abc/subFolderA</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwAAAQ==</v></e>
    // 	<e><k>/Inbox/abc/subFolderB</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwMAAAA=</v></e>
    // 	<e><k>/Archive</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAG8XunwAAAA=</v></e>
    // 	<e><k>/Deleted Items</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEKAAAA</v></e>
    // 	<e><k>/Drafts</k><v>AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEPAAAA</v></e>
    // 	</hashtable>

    // We'll iterate over the folders, and search all folders beginning with "/Inbox" (effectively, we're recursively searching
    // everything under Inbox)
    CkXml xmlMap;
    bool success = xmlMap.LoadXmlFile("qa_data/outlook/folderMap.xml");
    if (success != true) {
        std::cout << "Failed to load XML folder map." << "\r\n";
        return;
    }

    // We're going to return just the message id, subject, and FROM name/address.
    http.SetUrlVar("select","id,subject,from");

    // Our search will be for emails with the word "sample" in the subject.
    http.SetUrlVar("filter","contains(subject,'sample')");

    CkJsonObject json;
    json.put_EmitCompact(false);

    CkJsonObject jsonCombined;
    jsonCombined.put_EmitCompact(false);

    CkStringBuilder sbResponse;
    CkStringBuilder sbPath;
    int numFolders = xmlMap.get_NumChildren();
    int i = 0;
    int j = 0;
    int k = 0;
    while (i < numFolders) {
        xmlMap.put_I(i);
        sbPath.SetString(xmlMap.getChildContent("e[i]|k"));

        if (sbPath.StartsWith("/Inbox",true) == true) {

            std::cout << "------------------------------------------------------------------" << "\r\n";
            std::cout << "Searching " << sbPath.getAsString() << "\r\n";

            // Search this mail folder..
            http.SetUrlVar("folder_id",xmlMap.getChildContent("e[i]|v"));

            sbResponse.Clear();
            success = http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse);
            if ((success != true) && (http.get_LastStatus() == 0)) {
                std::cout << http.lastErrorText() << "\r\n";
                return;
            }

            json.LoadSb(sbResponse);

            if (http.get_LastStatus() != 200) {
                std::cout << "HTTP response status = " << http.get_LastStatus() << "\r\n";
                std::cout << json.emit() << "\r\n";
                std::cout << "Failed." << "\r\n";
                return;
            }

            std::cout << json.emit() << "\r\n";

            // Each mail folder search will return JSON with a value array, which is non-empty if any matching messages were found:

            // 			{
            // 			  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA')/messages(id,subject,from)",
            // 			  "value": [
            // 				Individual messages, if any, are listed here.
            // 			  ]
            // 			}

            int numMessages = json.SizeOfArray("value");
            j = 0;
            while (j < numMessages) {
                json.put_J(j);

                // Add this message to the final result set.
                jsonCombined.put_K(k);
                jsonCombined.UpdateString("value[k].folderPath",sbPath.getAsString());
                jsonCombined.UpdateString("value[k].id",json.stringOf("value[j].id"));
                jsonCombined.UpdateString("value[k].subject",json.stringOf("value[j].subject"));
                jsonCombined.UpdateString("value[k].from.emailAddress.name",json.stringOf("value[j].from.emailAddress.name"));
                jsonCombined.UpdateString("value[k].from.emailAddress.address",json.stringOf("value[j].from.emailAddress.address"));
                k = k + 1;

                j = j + 1;
            }

        }

        i = i + 1;
    }

    // Show the final combined JSON search result.
    std::cout << "------------------------------------------------------------------" << "\r\n";
    std::cout << "Combined Search Results:" << "\r\n";
    std::cout << jsonCombined.emit() << "\r\n";

    // Sample output for the above program:

    // ------------------------------------------------------------------
    // Searching /Inbox
    // {
    //   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA')/messages(id,subject,from)",
    //   "value": [
    //     {
    //       "@odata.etag": "W/\"CQAAABYAAADn68XtMop0TpsYJGpfKXY9AADOpwfr\"",
    //       "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA5_vF7TKKdE6bGCRqXyl2PQAAAM6Jj10AAAA=",
    //       "subject": "A sample email with Amazon in the body",
    //       "from": {
    //         "emailAddress": {
    //           "name": "Chilkat Software",
    //           "address": "support@chilkatsoft.com"
    //         }
    //       }
    //     }
    //   ]
    // }
    // 
    // ------------------------------------------------------------------
    // Searching /Inbox/xyz
    // {
    //   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwEAAAA%3D')/messages(id,subject,from)",
    //   "value": [
    //   ]
    // }
    // 
    // ------------------------------------------------------------------
    // Searching /Inbox/abc/subFolderA/a
    // {
    //   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwIAAAA%3D')/messages(id,subject,from)",
    //   "value": [
    //     {
    //       "@odata.etag": "W/\"CQAAABYAAADn68XtMop0TpsYJGpfKXY9AADOpzfb\"",
    //       "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwIAAADn68XtMop0TpsYJGpfKXY9AAAAzombFQAAAA==",
    //       "subject": "Sample email from admin@chilkat.io",
    //       "from": {
    //         "emailAddress": {
    //           "name": "Chilkat Software",
    //           "address": "admin@chilkat.io"
    //         }
    //       }
    //     }
    //   ]
    // }
    // 
    // ------------------------------------------------------------------
    // Searching /Inbox/abc
    // {
    //   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huv8AAAA%3D')/messages(id,subject,from)",
    //   "value": [
    //   ]
    // }
    // 
    // ------------------------------------------------------------------
    // Searching /Inbox/abc/subFolderA
    // {
    //   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwAAAQ%3D%3D')/messages(id,subject,from)",
    //   "value": [
    //   ]
    // }
    // 
    // ------------------------------------------------------------------
    // Searching /Inbox/abc/subFolderB
    // {
    //   "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('me')/mailFolders('AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgAuAAADsVyfxjDU406Ic4X7ill8xAEA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwMAAAA%3D')/messages(id,subject,from)",
    //   "value": [
    //   ]
    // }
    // 
    // ------------------------------------------------------------------
    // Combined Search Results:
    // {
    //   "value": [
    //     {
    //       "folderPath": "/Inbox",
    //       "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAgEMAAAA5_vF7TKKdE6bGCRqXyl2PQAAAM6Jj10AAAA=",
    //       "subject": "A sample email with Amazon in the body",
    //       "from": {
    //         "emailAddress": {
    //           "name": "Chilkat Software",
    //           "address": "support@chilkatsoft.com"
    //         }
    //       }
    //     },
    //     {
    //       "folderPath": "/Inbox/abc/subFolderA/a",
    //       "id": "AQMkADAwATM0MDAAMS1iNTcwLWI2NTEtMDACLTAwCgBGAAADsVyfxjDU406Ic4X7ill8xAcA5_vF7TKKdE6bGCRqXyl2PQAAAL8huwIAAADn68XtMop0TpsYJGpfKXY9AAAAzombFQAAAA==",
    //       "subject": "Sample email from admin@chilkat.io",
    //       "from": {
    //         "emailAddress": {
    //           "name": "Chilkat Software",
    //           "address": "admin@chilkat.io"
    //         }
    //       }
    //     }
    //   ]
    // }
    // 
    }

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.