PureBasic
PureBasic
Fetch Full Email Given Email Header
When you fetch email headers using UIDs instead of sequence numbers, the email object (which includes only the header) will have auto-generatedckx-imap-* headers. These headers provide details like the UID and attachments. The IMAP UID is found in the ckx-imap-uid header. Additionally, the ckx-imap-isUid header indicates whether the email header was downloaded by UID, showing YES or NO. Since sequence numbers can change if emails are deleted, UIDs are essential for downloading the correct full email.
The Chilkat Email object offers a GetImapUid method to retrieve the UID from the ckx-imap-uid header. This UID can be used to fetch the full email.
Chilkat PureBasic Downloads
IncludeFile "CkEmail.pb"
IncludeFile "CkImap.pb"
Procedure ChilkatExample()
success.i = 0
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
imap.i = CkImap::ckCreate()
If imap.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Connect to an IMAP server.
; Use TLS
CkImap::setCkSsl(imap, 1)
CkImap::setCkPort(imap, 993)
success = CkImap::ckConnect(imap,"imap.example.com")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
; Login
success = CkImap::ckLogin(imap,"***","***")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
; Select an IMAP mailbox
success = CkImap::ckSelectMailbox(imap,"Inbox")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
emailHeader.i = CkEmail::ckCreate()
If emailHeader.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
emailFull.i = CkEmail::ckCreate()
If emailFull.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
uid.i = 2014
isUid.i = 1
; Fetch only the email header
success = CkImap::ckFetchEmail(imap,1,uid,isUid,emailHeader)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkEmail::ckDispose(emailHeader)
CkEmail::ckDispose(emailFull)
ProcedureReturn
EndIf
; Now fetch the full email
uidFromCkxHeader.i = CkEmail::ckGetImapUid(emailHeader)
If uidFromCkxHeader < 0
; Failed.
Debug "No ckx-imap-uid header was found."
CkImap::ckDispose(imap)
CkEmail::ckDispose(emailHeader)
CkEmail::ckDispose(emailFull)
ProcedureReturn
EndIf
success = CkImap::ckFetchEmail(imap,0,uid,isUid,emailFull)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkEmail::ckDispose(emailHeader)
CkEmail::ckDispose(emailFull)
ProcedureReturn
EndIf
; OK, we have the full email, do whatever we want...
; Disconnect from the IMAP server.
success = CkImap::ckDisconnect(imap)
CkImap::ckDispose(imap)
CkEmail::ckDispose(emailHeader)
CkEmail::ckDispose(emailFull)
ProcedureReturn
EndProcedure