PureBasic
PureBasic
Get IMAP Attachment Filenames
See more IMAP Examples
Demonstrates the Chilkat Imap.GetMailAttachFilename method, which returns the filename of an attachment. The first argument is the Email and the second is the zero-based attachment index. This example fetches only the message headers and lists the attachment filenames.
Background: This method reads the filename from
ckx-imap-* metadata, so it works on a header-only email even though the attachment bodies have not been downloaded. That is exactly what a mail client needs to render the paperclip list — the names of the attached files — before the user decides to download any of them.Chilkat PureBasic Downloads
IncludeFile "CkEmail.pb"
IncludeFile "CkImap.pb"
Procedure ChilkatExample()
success.i = 0
; Demonstrates the Imap.GetMailAttachFilename method, which returns the filename of an
; attachment. The 1st argument is the Email and the 2nd is the zero-based attachment index.
imap.i = CkImap::ckCreate()
If imap.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
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
success = CkImap::ckLogin(imap,"user@example.com","myPassword")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
success = CkImap::ckSelectMailbox(imap,"Inbox")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
; Fetch only the message headers. Attachment bodies are NOT downloaded, but the ckx-imap-*
; metadata describing the attachments is present, so the attachment info methods still work.
headersOnly.i = 1
useUid.i = 0
seqNum.i = 1
email.i = CkEmail::ckCreate()
If email.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkImap::ckFetchEmail(imap,headersOnly,seqNum,useUid,email)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkEmail::ckDispose(email)
ProcedureReturn
EndIf
; Loop over the attachments and print each filename.
numAttach.i = CkImap::ckGetMailNumAttach(imap,email)
Debug "Attachments: " + Str(numAttach)
i.i
For i = 0 To numAttach - 1
; GetMailAttachFilename returns a string, so assign it to a string variable.
fname.s = CkImap::ckGetMailAttachFilename(imap,email,i)
If CkImap::ckLastMethodSuccess(imap) = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkEmail::ckDispose(email)
ProcedureReturn
EndIf
Debug fname
Next
success = CkImap::ckDisconnect(imap)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkEmail::ckDispose(email)
ProcedureReturn
EndIf
CkImap::ckDispose(imap)
CkEmail::ckDispose(email)
ProcedureReturn
EndProcedure