Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) Outlook -- Search Messages in a Particular FolderDemonstrates search the messages in a particular Outlook mailbox folder. This uses the OData $filter and $search system query options. See OData System Query Options for general information.
Also see OData URL Conventions for information about $filter, $search and other query options. This example demonstrates the following searches:
This example applies to: Exchange Online | Office 365 | Hotmail.com | Live.com | MSN.com | Outlook.com | Passport.com
integer li_rc oleobject loo_Http oleobject loo_SbResponse oleobject loo_HtFolderMap oleobject loo_SbMap string ls_FolderId integer li_Success oleobject loo_Json oleobject loo_SbExpression oleobject loo_Dt oleobject loo_DtObj // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loo_Http = create oleobject // Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0 li_rc = loo_Http.ConnectToNewObject("Chilkat.Http") if li_rc < 0 then destroy loo_Http MessageBox("Error","Connecting to COM object failed") return end if // 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). loo_Http.AuthToken = "MICROSOFT_GRAPH_ACCESS_TOKEN" loo_SbResponse = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbResponse.ConnectToNewObject("Chilkat.StringBuilder") // In this example, we'd like to get the messages in the folder "/Inbox", // but we must specify the corresponding folder_id. The best way to do this is to create // a local map of folderPaths-to-folderIds. // This example does it: Create Outlook Folder Map) loo_HtFolderMap = create oleobject // Use "Chilkat_9_5_0.Hashtable" for versions of Chilkat < 10.0.0 li_rc = loo_HtFolderMap.ConnectToNewObject("Chilkat.Hashtable") loo_SbMap = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbMap.ConnectToNewObject("Chilkat.StringBuilder") loo_SbMap.LoadFile("qa_data/outlook/folderMap.xml","utf-8") loo_HtFolderMap.AddFromXmlSb(loo_SbMap) // Get the ID for the "/Inbox" folder: ls_FolderId = loo_HtFolderMap.LookupStr("/Inbox") if loo_HtFolderMap.LastMethodSuccess <> 1 then Write-Debug "Folder ID not found" destroy loo_Http destroy loo_SbResponse destroy loo_HtFolderMap destroy loo_SbMap return end if li_Success = 1 loo_Json = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject") loo_Json.EmitCompact = 0 loo_Http.SetUrlVar("folder_id",ls_FolderId) loo_Http.SetUrlVar("select","subject,from") // ----------------------------------------------------------------------------------------------------- // Only return emails from "chilkat.support@gmail.com" loo_Http.SetUrlVar("filter","from/emailAddress/address eq 'chilkat.support@gmail.com'") // To return the full content of each email, omit the "&select=..." part of the URL. li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",loo_SbResponse) if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then Write-Debug loo_Http.LastErrorText destroy loo_Http destroy loo_SbResponse destroy loo_HtFolderMap destroy loo_SbMap destroy loo_Json return end if loo_Json.LoadSb(loo_SbResponse) Write-Debug loo_Json.Emit() // ----------------------------------------------------------------------------------------------------- // Only return emails where the subject contains "Amazon" loo_Http.SetUrlVar("filter","contains(subject,'Amazon')") li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",loo_SbResponse) if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then Write-Debug loo_Http.LastErrorText destroy loo_Http destroy loo_SbResponse destroy loo_HtFolderMap destroy loo_SbMap destroy loo_Json return end if loo_Json.LoadSb(loo_SbResponse) Write-Debug loo_Json.Emit() // ----------------------------------------------------------------------------------------------------- // Only return emails where the subject starts with "this email" and the from address is support@chilkatsoft.com // (the startswith function is case insensitive) loo_Http.SetUrlVar("filter","startswith(subject,'this email') and (from/emailAddress/address eq 'support@chilkatsoft.com')") li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",loo_SbResponse) if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then Write-Debug loo_Http.LastErrorText destroy loo_Http destroy loo_SbResponse destroy loo_HtFolderMap destroy loo_SbMap destroy loo_Json return end if loo_Json.LoadSb(loo_SbResponse) Write-Debug loo_Json.Emit() // ----------------------------------------------------------------------------------------------------- // Only return emails received within the last 24 hours loo_SbExpression = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_SbExpression.ConnectToNewObject("Chilkat.StringBuilder") loo_SbExpression.Append("receivedDateTime ge ") loo_Dt = create oleobject // Use "Chilkat_9_5_0.CkDateTime" for versions of Chilkat < 10.0.0 li_rc = loo_Dt.ConnectToNewObject("Chilkat.CkDateTime") loo_Dt.SetFromCurrentSystemTime() loo_Dt.AddDays(-1) loo_SbExpression.Append(loo_Dt.GetAsTimestamp(0)) loo_Http.SetUrlVar("filter",loo_SbExpression.GetAsString()) li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",loo_SbResponse) if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then Write-Debug loo_Http.LastErrorText destroy loo_Http destroy loo_SbResponse destroy loo_HtFolderMap destroy loo_SbMap destroy loo_Json destroy loo_SbExpression destroy loo_Dt return end if loo_Json.LoadSb(loo_SbResponse) Write-Debug loo_Json.Emit() // ----------------------------------------------------------------------------------------------------- // Only return emails received in October 2016 loo_DtObj = create oleobject // Use "Chilkat_9_5_0.DtObj" for versions of Chilkat < 10.0.0 li_rc = loo_DtObj.ConnectToNewObject("Chilkat.DtObj") loo_DtObj.Year = 2016 loo_DtObj.Month = 10 loo_DtObj.Day = 1 loo_DtObj.Utc = 1 loo_Dt.SetFromDtObj(loo_DtObj) loo_SbExpression.Clear() loo_SbExpression.Append("(receivedDateTime ge ") loo_SbExpression.Append(loo_Dt.GetAsTimestamp(0)) loo_SbExpression.Append(") and (receivedDateTime lt ") loo_DtObj.Month = 11 loo_Dt.SetFromDtObj(loo_DtObj) loo_SbExpression.Append(loo_Dt.GetAsTimestamp(0)) loo_SbExpression.Append(")") // This is the expression we just built: (receivedDateTime ge 2016-10-01T00:00:00Z) and (receivedDateTime lt 2016-11-01T00:00:00Z) Write-Debug loo_SbExpression.GetAsString() loo_Http.SetUrlVar("filter",loo_SbExpression.GetAsString()) li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",loo_SbResponse) if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then Write-Debug loo_Http.LastErrorText destroy loo_Http destroy loo_SbResponse destroy loo_HtFolderMap destroy loo_SbMap destroy loo_Json destroy loo_SbExpression destroy loo_Dt destroy loo_DtObj return end if loo_Json.LoadSb(loo_SbResponse) Write-Debug loo_Json.Emit() // ----------------------------------------------------------------------------------------------------- // Return unread emails loo_Http.SetUrlVar("filter","isRead eq false") // success = http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse); if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then Write-Debug loo_Http.LastErrorText destroy loo_Http destroy loo_SbResponse destroy loo_HtFolderMap destroy loo_SbMap destroy loo_Json destroy loo_SbExpression destroy loo_Dt destroy loo_DtObj return end if loo_Json.LoadSb(loo_SbResponse) Write-Debug loo_Json.Emit() // ----------------------------------------------------------------------------------------------------- // Return emails with a plain-text or HTML body containing the phrase "Outlook 365" loo_Http.SetUrlVar("filter","contains(body/content,'Outlook 365')") li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",loo_SbResponse) if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then Write-Debug loo_Http.LastErrorText destroy loo_Http destroy loo_SbResponse destroy loo_HtFolderMap destroy loo_SbMap destroy loo_Json destroy loo_SbExpression destroy loo_Dt destroy loo_DtObj return end if loo_Json.LoadSb(loo_SbResponse) Write-Debug loo_Json.Emit() // ----------------------------------------------------------------------------------------------------- // Use the $search query option instead of $filter. // $search is a free-text search over whatever fields the server deems appropriate, such as in the subject, // body, address fields, etc. // Search for the word Amazon loo_Http.SetUrlVar("search","Amazon") li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$search={$search}&$select={$select}",loo_SbResponse) if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then Write-Debug loo_Http.LastErrorText destroy loo_Http destroy loo_SbResponse destroy loo_HtFolderMap destroy loo_SbMap destroy loo_Json destroy loo_SbExpression destroy loo_Dt destroy loo_DtObj return end if loo_Json.LoadSb(loo_SbResponse) Write-Debug loo_Json.Emit() // ----------------------------------------------------------------------------------------------------- // Search for chilkatsoft.com // Some chars, such as the "." make it necessary to enclose the search expression in double-quotes. loo_Http.SetUrlVar("search","~"chilkatsoft.com~"") li_Success = loo_Http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$search={$search}&$select={$select}",loo_SbResponse) if (li_Success <> 1) AND (loo_Http.LastStatus = 0) then Write-Debug loo_Http.LastErrorText destroy loo_Http destroy loo_SbResponse destroy loo_HtFolderMap destroy loo_SbMap destroy loo_Json destroy loo_SbExpression destroy loo_Dt destroy loo_DtObj return end if loo_Json.LoadSb(loo_SbResponse) Write-Debug loo_Json.Emit() destroy loo_Http destroy loo_SbResponse destroy loo_HtFolderMap destroy loo_SbMap destroy loo_Json destroy loo_SbExpression destroy loo_Dt destroy loo_DtObj |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.