DataFlex
DataFlex
Outlook -- Search Messages in a Particular Folder
See more Outlook Examples
Demonstrates 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:
- Find emails from a particular email address.
- Find emails where the subject contains a particular word or phrase.
- Using an "AND" expression.
- Find emails received in the last 24 hours.
- Find emails received in October 2016
- Find unread emails
- Find emails containing a particular phrase in the body.
- Free-text search of a keyword or phrase.
- Free-text search of an email address.
This example applies to: Exchange Online | Office 365 | Hotmail.com | Live.com | MSN.com | Outlook.com | Passport.com
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
Variant vSbResponse
Handle hoSbResponse
Handle hoHtFolderMap
Variant vSbMap
Handle hoSbMap
String sFolderId
Handle hoJson
Handle hoSbExpression
Handle hoDt
Obj Handle hoDtObj
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
// 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).
Set ComAuthToken Of hoHttp To "MICROSOFT_GRAPH_ACCESS_TOKEN"
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponse
If (Not(IsComObjectCreated(hoSbResponse))) Begin
Send CreateComObject of hoSbResponse
End
// 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)
Get Create (RefClass(cComChilkatHashtable)) To hoHtFolderMap
If (Not(IsComObjectCreated(hoHtFolderMap))) Begin
Send CreateComObject of hoHtFolderMap
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbMap
If (Not(IsComObjectCreated(hoSbMap))) Begin
Send CreateComObject of hoSbMap
End
Get ComLoadFile Of hoSbMap "qa_data/outlook/folderMap.xml" "utf-8" To iSuccess
Get pvComObject of hoSbMap to vSbMap
Get ComAddFromXmlSb Of hoHtFolderMap vSbMap To iSuccess
// Get the ID for the "/Inbox" folder:
Get ComLookupStr Of hoHtFolderMap "/Inbox" To sFolderId
Get ComLastMethodSuccess Of hoHtFolderMap To bTemp1
If (bTemp1 <> True) Begin
Showln "Folder ID not found"
Procedure_Return
End
Move True To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Set ComEmitCompact Of hoJson To False
Get ComSetUrlVar Of hoHttp "folder_id" sFolderId To iSuccess
Get ComSetUrlVar Of hoHttp "select" "subject,from" To iSuccess
// -----------------------------------------------------------------------------------------------------
// Only return emails from "chilkat.support@gmail.com"
Get ComSetUrlVar Of hoHttp "filter" "from/emailAddress/address eq 'chilkat.support@gmail.com'" To iSuccess
// To return the full content of each email, omit the "&select=..." part of the URL.
Get pvComObject of hoSbResponse to vSbResponse
Get ComQuickGetSb Of hoHttp "https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}" vSbResponse To iSuccess
Get ComLastStatus Of hoHttp To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoSbResponse to vSbResponse
Get ComLoadSb Of hoJson vSbResponse To iSuccess
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
// -----------------------------------------------------------------------------------------------------
// Only return emails where the subject contains "Amazon"
Get ComSetUrlVar Of hoHttp "filter" "contains(subject,'Amazon')" To iSuccess
Get pvComObject of hoSbResponse to vSbResponse
Get ComQuickGetSb Of hoHttp "https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}" vSbResponse To iSuccess
Get ComLastStatus Of hoHttp To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoSbResponse to vSbResponse
Get ComLoadSb Of hoJson vSbResponse To iSuccess
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
// -----------------------------------------------------------------------------------------------------
// Only return emails where the subject starts with "this email" and the from address is support@chilkatsoft.com
// (the startswith function is case insensitive)
Get ComSetUrlVar Of hoHttp "filter" "startswith(subject,'this email') and (from/emailAddress/address eq 'support@chilkatsoft.com')" To iSuccess
Get pvComObject of hoSbResponse to vSbResponse
Get ComQuickGetSb Of hoHttp "https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}" vSbResponse To iSuccess
Get ComLastStatus Of hoHttp To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoSbResponse to vSbResponse
Get ComLoadSb Of hoJson vSbResponse To iSuccess
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
// -----------------------------------------------------------------------------------------------------
// Only return emails received within the last 24 hours
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbExpression
If (Not(IsComObjectCreated(hoSbExpression))) Begin
Send CreateComObject of hoSbExpression
End
Get ComAppend Of hoSbExpression "receivedDateTime ge " To iSuccess
Get Create (RefClass(cComCkDateTime)) To hoDt
If (Not(IsComObjectCreated(hoDt))) Begin
Send CreateComObject of hoDt
End
Get ComSetFromCurrentSystemTime Of hoDt To iSuccess
Get ComAddDays Of hoDt -1 To iSuccess
Get ComGetAsTimestamp Of hoDt False To sTemp1
Get ComAppend Of hoSbExpression sTemp1 To iSuccess
Get ComGetAsString Of hoSbExpression To sTemp1
Get ComSetUrlVar Of hoHttp "filter" sTemp1 To iSuccess
Get pvComObject of hoSbResponse to vSbResponse
Get ComQuickGetSb Of hoHttp "https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}" vSbResponse To iSuccess
Get ComLastStatus Of hoHttp To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoSbResponse to vSbResponse
Get ComLoadSb Of hoJson vSbResponse To iSuccess
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
// -----------------------------------------------------------------------------------------------------
// Only return emails received in October 2016
Get Create (RefClass(cComChilkatDtObj)) To hoDtObj
If (Not(IsComObjectCreated(hoDtObj))) Begin
Send CreateComObject of hoDtObj
End
Set ComYear Of hoDtObj To 2016
Set ComMonth Of hoDtObj To 10
Set ComDay Of hoDtObj To 1
Set ComUtc Of hoDtObj To True
Get pvComObject of hoDtObj to vDtObj
Get ComSetFromDtObj Of hoDt vDtObj To iSuccess
Send ComClear To hoSbExpression
Get ComAppend Of hoSbExpression "(receivedDateTime ge " To iSuccess
Get ComGetAsTimestamp Of hoDt False To sTemp1
Get ComAppend Of hoSbExpression sTemp1 To iSuccess
Get ComAppend Of hoSbExpression ") and (receivedDateTime lt " To iSuccess
Set ComMonth Of hoDtObj To 11
Get pvComObject of hoDtObj to vDtObj
Get ComSetFromDtObj Of hoDt vDtObj To iSuccess
Get ComGetAsTimestamp Of hoDt False To sTemp1
Get ComAppend Of hoSbExpression sTemp1 To iSuccess
Get ComAppend Of hoSbExpression ")" To iSuccess
// This is the expression we just built: (receivedDateTime ge 2016-10-01T00:00:00Z) and (receivedDateTime lt 2016-11-01T00:00:00Z)
Get ComGetAsString Of hoSbExpression To sTemp1
Showln sTemp1
Get ComGetAsString Of hoSbExpression To sTemp1
Get ComSetUrlVar Of hoHttp "filter" sTemp1 To iSuccess
Get pvComObject of hoSbResponse to vSbResponse
Get ComQuickGetSb Of hoHttp "https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}" vSbResponse To iSuccess
Get ComLastStatus Of hoHttp To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoSbResponse to vSbResponse
Get ComLoadSb Of hoJson vSbResponse To iSuccess
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
// -----------------------------------------------------------------------------------------------------
// Return unread emails
Get ComSetUrlVar Of hoHttp "filter" "isRead eq false" To iSuccess
// success = http.QuickGetSb("https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}",sbResponse);
Get ComLastStatus Of hoHttp To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoSbResponse to vSbResponse
Get ComLoadSb Of hoJson vSbResponse To iSuccess
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
// -----------------------------------------------------------------------------------------------------
// Return emails with a plain-text or HTML body containing the phrase "Outlook 365"
Get ComSetUrlVar Of hoHttp "filter" "contains(body/content,'Outlook 365')" To iSuccess
Get pvComObject of hoSbResponse to vSbResponse
Get ComQuickGetSb Of hoHttp "https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$filter={$filter}&$select={$select}" vSbResponse To iSuccess
Get ComLastStatus Of hoHttp To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoSbResponse to vSbResponse
Get ComLoadSb Of hoJson vSbResponse To iSuccess
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
// -----------------------------------------------------------------------------------------------------
// 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
Get ComSetUrlVar Of hoHttp "search" "Amazon" To iSuccess
Get pvComObject of hoSbResponse to vSbResponse
Get ComQuickGetSb Of hoHttp "https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$search={$search}&$select={$select}" vSbResponse To iSuccess
Get ComLastStatus Of hoHttp To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoSbResponse to vSbResponse
Get ComLoadSb Of hoJson vSbResponse To iSuccess
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
// -----------------------------------------------------------------------------------------------------
// Search for chilkatsoft.com
// Some chars, such as the "." make it necessary to enclose the search expression in double-quotes.
Get ComSetUrlVar Of hoHttp "search" '"chilkatsoft.com"' To iSuccess
Get pvComObject of hoSbResponse to vSbResponse
Get ComQuickGetSb Of hoHttp "https://graph.microsoft.com/v1.0/me/mailFolders/{$folder_id}/messages?$search={$search}&$select={$select}" vSbResponse To iSuccess
Get ComLastStatus Of hoHttp To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get pvComObject of hoSbResponse to vSbResponse
Get ComLoadSb Of hoJson vSbResponse To iSuccess
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
End_Procedure