DataFlex
DataFlex
Search IMAP Mailbox for Email Matching Criteria
Searching an IMAP mailbox for messages that match search criteria.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoImap
Boolean iFetchUids
String sAllMsgs
String sAnswered
String sOnDate
String sBetweenDates
String sComplexSearch1
String sBodySearch
String sOrSearch
String sFromSearch
String sJohnSearch
String sRecentSearch
String sNotRecentSearch
String sOldSearch
String sMarkedForDeleteSearch
String sHeaderSearch
String sHeaderExistsSearch
String sNewSearch
String sSizeLargerSearch
String sSeenSearch
String sNotSeenSearch
String sToSearch
String sToSearch2
String sSmallerSearch
String sFullSubstringSearch
Variant vMessageSet
Handle hoMessageSet
Variant vBundle
Handle hoBundle
Boolean iHeadersOnly
Variant vEmail
Handle hoEmail
Integer i
String sTemp1
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(cComChilkatImap)) To hoImap
If (Not(IsComObjectCreated(hoImap))) Begin
Send CreateComObject of hoImap
End
// Connect to an IMAP server.
// Use TLS
Set ComSsl Of hoImap To True
Set ComPort Of hoImap To 993
Get ComConnect Of hoImap "imap.example.com" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
// Login
Get ComLogin Of hoImap "myLogin" "myPassword" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
// Select an IMAP mailbox
Get ComSelectMailbox Of hoImap "Inbox" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
// We can choose to fetch UIDs or sequence numbers.
Move True To iFetchUids
// Here are examples of different search criteria:
// Return all messages.
Move "ALL" To sAllMsgs
// Search for already-answered emails.
Move "ANSWERED" To sAnswered
// Search for messages on a specific date.
// The date string is DD-Month-YYYY where Month is
// Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, or Dec.
Move "SENTON 05-Mar-2007" To sOnDate
// Search for messages between two dates. SENTBEFORE
// finds emails sent before a date, and SENTSINCE finds
// email sent on or after a date. The "AND" operation
// is implied by joining criteria, separated by spaces.
Move "SENTSINCE 01-Mar-2007 SENTBEFORE 05-Mar-2007" To sBetweenDates
// Another example of AND: find all unanswered emails
// sent after 04-Mar-2007 with "Problem" in the subject:
Move 'UNANSWERED SENTSINCE 04-Mar-2007 Subject "Problem"' To sComplexSearch1
// Find messages with a specific string in the body:
Move 'BODY "problem solved"' To sBodySearch
// Using OR. The syntax is OR <criteria1> <criteria2>.
// The "OR" comes first, followed by each criteria.
// For example, to match all emails with "Help" or "Question" in the subject.
// You'll notice that literal strings may be quoted or unquoted.
// If a literal contains SPACE characters, quote it:
Move "OR SUBJECT Help SUBJECT Question" To sOrSearch
// ----------------------------------------------
// Strings are case-insensitive when searching....
// ----------------------------------------------
// Find all emails sent from yahoo.com addresses:
Move "FROM yahoo.com" To sFromSearch
// Find all emails sent from anyone with "John" in their name:
Move "FROM John" To sJohnSearch
// Find emails with the RECENT flag set:
Move "RECENT" To sRecentSearch
// Find emails that don't have the recent flag set:
Move "NOT RECENT" To sNotRecentSearch
// This is synonymous with "OLD":
Move "OLD" To sOldSearch
// Find all emails marked for deletion:
Move "DELETED" To sMarkedForDeleteSearch
// Find all emails having a specified header field with a value
// containing a substring:
Move "HEADER DomainKey-Signature paypal.com" To sHeaderSearch
// Find any emails having a specific header field. If the
// 2nd argument to the "HEADER" criteria is an empty string,
// any email having the header field is returned regardless
// of the header field's content.
// Find any emails with a DomainKey-Signature field:
Move 'HEADER DomainKey-Signature ""' To sHeaderExistsSearch
// Find NEW emails: these are emails that have the RECENT flag
// set, but not the SEEN flag:
Move "NEW" To sNewSearch
// Find emails larger than a certain number of bytes:
Move "LARGER 500000" To sSizeLargerSearch
// Find emails marked as seen or not already seen:
Move "SEEN" To sSeenSearch
Move "NOT SEEN" To sNotSeenSearch
// Find emails having a given substring in the TO header field:
Move "TO support@chilkatsoft.com" To sToSearch
// A more long-winded way to do the same thing:
Move "HEADER TO support@chilkatsoft.com" To sToSearch2
// Find emails smaller than a size in bytes:
Move "SMALLER 30000" To sSmallerSearch
// Find emails that have a substring anywhere in the header
// or body:
Move 'TEXT "Zip Component"' To sFullSubstringSearch
// Pass any of the above strings here to test a search:
Get Create (RefClass(cComChilkatMessageSet)) To hoMessageSet
If (Not(IsComObjectCreated(hoMessageSet))) Begin
Send CreateComObject of hoMessageSet
End
Get pvComObject of hoMessageSet to vMessageSet
Get ComQueryMbx Of hoImap sOrSearch iFetchUids vMessageSet To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
// Fetch the email headers into a bundle object:
Get Create (RefClass(cComChilkatEmailBundle)) To hoBundle
If (Not(IsComObjectCreated(hoBundle))) Begin
Send CreateComObject of hoBundle
End
Move True To iHeadersOnly
Get pvComObject of hoMessageSet to vMessageSet
Get pvComObject of hoBundle to vBundle
Get ComFetchMsgSet Of hoImap iHeadersOnly vMessageSet vBundle To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
// Display the Subject and From of each email.
Get Create (RefClass(cComChilkatEmail)) To hoEmail
If (Not(IsComObjectCreated(hoEmail))) Begin
Send CreateComObject of hoEmail
End
Move 0 To i
While (i < (ComMessageCount(hoBundle)))
Get pvComObject of hoEmail to vEmail
Get ComEmailAt Of hoBundle i vEmail To iSuccess
Get ComGetHeaderField Of hoEmail "Date" To sTemp1
Showln sTemp1
Get ComSubject Of hoEmail To sTemp1
Showln sTemp1
Get ComFrom Of hoEmail To sTemp1
Showln sTemp1
Showln "--"
Move (i + 1) To i
Loop
// Disconnect from the IMAP server.
Get ComDisconnect Of hoImap To iSuccess
End_Procedure