DataFlex
DataFlex
Get IMAP Message Threads as JSON
See more IMAP Examples
Demonstrates the Chilkat Imap.QueryThread method, which sends the IMAP THREAD command for the selected mailbox and returns the thread hierarchy as JSON. The first argument is the threading algorithm (commonly REFERENCES or ORDEREDSUBJECT), the second is IMAP search criteria, and the third (bUid) selects UID vs sequence-number identifiers. The returned JSON has a top-level threads array whose nested arrays encode parent/child relationships. This example threads all messages in the Inbox.
Tip: JSON parsing code for this result can be generated at Chilkat Tools.
Background: "Conversation view" in a mail client — grouping a message with its replies and follow-ups — is exactly what the IMAP
THREAD extension provides, computed on the server. REFERENCES threads by the References/In-Reply-To headers (the accurate way), while ORDEREDSUBJECT groups by subject. Not all servers support THREAD, so check HasCapability for it first.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoImap
Variant vJson
Handle hoJson
String sTemp1
Move False To iSuccess
// Demonstrates the Imap.QueryThread method, which sends the IMAP THREAD command for the
// selected mailbox and returns the thread hierarchy as JSON. The 1st argument is the
// threading algorithm (such as REFERENCES or ORDEREDSUBJECT), the 2nd is IMAP search
// criteria, and the 3rd (bUid) selects UID vs sequence-number identifiers.
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
Get ComConnect Of hoImap "imap.example.com" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComLogin Of hoImap "user@example.com" "myPassword" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComSelectMailbox Of hoImap "Inbox" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
// Group all messages into threads using the REFERENCES algorithm, returning UIDs.
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get pvComObject of hoJson to vJson
Get ComQueryThread Of hoImap "REFERENCES" "ALL" True vJson To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
Set ComEmitCompact Of hoJson To False
Get ComEmit Of hoJson To sTemp1
Showln sTemp1
// JSON parsing code for this result can be generated at Chilkat's online tool:
// https://tools.chilkat.io/jsonParse
Get ComDisconnect Of hoImap To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
End_Procedure