DataFlex
DataFlex
Fetch Inbox Email Headers
Downloads the headers of all emails in the Inbox and shows some information about each, such as From, Subject, and whether the email has been seen (already read) or not.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoImap
Integer iCount
Variant vBundle
Handle hoBundle
Variant vEmailHeader
Handle hoEmailHeader
Integer i
Integer iSeenFlag
String sTemp1
Integer iTemp1
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
// Normally, when an email is downloaded, its "Seen" flag is automatically set.
// We don't want our program to be interfering with the "Seen" flags.
// To do this, set the PeekMode to True.
Set ComPeekMode Of hoImap To True
// After selecting the mailbox, the NumMessages property
// will contain the number of emails in the mailbox.
// When sequence numbers (not UIDs) are used to reference emails,
// they range from 1 to N, where N is the number of messages in the mailbox.
// This example will download the headers by sequence numbers.
Get ComNumMessages Of hoImap To iCount
Get Create (RefClass(cComChilkatEmailBundle)) To hoBundle
If (Not(IsComObjectCreated(hoBundle))) Begin
Send CreateComObject of hoBundle
End
Get pvComObject of hoBundle to vBundle
Get ComFetchRange Of hoImap True 1 iCount vBundle To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComMessageCount Of hoBundle To iTemp1
If (iTemp1 = 0) Begin
Showln "No messages in Inbox."
Procedure_Return
End
// Loop over the email headers.
Get Create (RefClass(cComChilkatEmail)) To hoEmailHeader
If (Not(IsComObjectCreated(hoEmailHeader))) Begin
Send CreateComObject of hoEmailHeader
End
Move 0 To i
While (i < (ComMessageCount(hoBundle)))
Get pvComObject of hoEmailHeader to vEmailHeader
Get ComEmailAt Of hoBundle i vEmailHeader To iSuccess
Get ComFrom Of hoEmailHeader To sTemp1
Showln sTemp1
Get ComSubject Of hoEmailHeader To sTemp1
Showln sTemp1
// Get the "Seen" flag.
// When an email is fetched from the IMAP server, the email flags are stored
// within the email in the "ckx-imap-flags" header.
// The call to GetMailFlag is simply getting the information from the email header.
// (it is not communicating with the IMAP server to get this information, because
// the information was obtained when downloading the headers)
Get pvComObject of hoEmailHeader to vEmailHeader
Get ComGetMailFlag Of hoImap vEmailHeader "Seen" To iSeenFlag
If (iSeenFlag = 1) Begin
Showln "This email has been read (already seen)"
End
Else Begin
Showln "This email has not yet been read (it is not yet seen)"
End
// We can also look directly at the "ckx-imap-flags" header:
Get ComGetHeaderField Of hoEmailHeader "ckx-imap-flags" To sTemp1
Showln "ckx-imap-flags: " sTemp1
Showln "--"
Move (i + 1) To i
Loop
// Disconnect from the IMAP server.
Get ComDisconnect Of hoImap To iSuccess
End_Procedure