DataFlex Requires Chilkat v11.0.0+
DataFlex
Find an email by subject and fetch it by IMAP UID
See more Email Object Examples
Search the selected INBOX by subject and fetch the first matching UID into an Email. The example requests complete attachment bodies and uses peek mode to preserve the read flag. Configure your own IMAP TLS endpoint and credentials.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoImap
Variant vMatches
Handle hoMatches
UInteger iUid
Variant vEmail
Handle hoEmail
String sTemp1
Integer iTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Get Create (RefClass(cComChilkatImap)) To hoImap
If (Not(IsComObjectCreated(hoImap))) Begin
Send CreateComObject of hoImap
End
Set ComSsl Of hoImap To True
Set ComPort Of hoImap To 993
Set ComRequireSslCertVerify Of hoImap To True
Set ComPeekMode Of hoImap To True
Set ComAutoDownloadAttachments Of hoImap To True
Get ComConnect Of hoImap "imap.example.com" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComLogin Of hoImap "MAIL_USER" "MAIL_PASSWORD" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComSelectMailbox Of hoImap "INBOX" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatMessageSet)) To hoMatches
If (Not(IsComObjectCreated(hoMatches))) Begin
Send CreateComObject of hoMatches
End
Get pvComObject of hoMatches to vMatches
Get ComQueryMbx Of hoImap 'SUBJECT "Sample message"' True vMatches To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComCount Of hoMatches To iTemp1
If (iTemp1 = 0) Begin
Showln "No matching message in INBOX."
Get ComDisconnect Of hoImap To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
Procedure_Return
End
// The search returned UIDs. Use the same identifier type for the fetch.
Get ComGetId Of hoMatches 0 To iUid
Get Create (RefClass(cComChilkatEmail)) To hoEmail
If (Not(IsComObjectCreated(hoEmail))) Begin
Send CreateComObject of hoEmail
End
Get pvComObject of hoEmail to vEmail
Get ComFetchEmail Of hoImap False iUid True vEmail To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComUidValidity Of hoImap To iTemp1
Showln "Mailbox INBOX, UIDVALIDITY " iTemp1 ", UID " iUid
Get ComDisconnect Of hoImap To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
// The downloaded Email remains usable after disconnecting.
Get ComSubject Of hoEmail To sTemp1
Get ComNumAttachments Of hoEmail To iTemp1
Showln "Subject: " sTemp1 " Files: " iTemp1
Get ComSaveEml Of hoEmail "imap-received.eml" To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoEmail To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Saved imap-received.eml."
End_Procedure