DataFlex
DataFlex
Save an IMAP Attachment to a File
See more IMAP Examples
Demonstrates the Chilkat Imap.FetchAttachment method, which downloads one attachment from the IMAP server and saves it to the filesystem. The first argument is the Email, the second is the zero-based attachment index, and the third is the file path to write. This example fetches a message headers-only, inspects its attachments without downloading them, and then downloads a specific attachment on demand.
Note: The output path is relative to the application's current working directory. Chilkat does not create missing parent directories, so qa_output/ must already exist.
Background: This is the method's real value. A headers-only fetch downloads no attachment bodies, yet methods such as
GetMailNumAttach and GetMailAttachFilename still work from the ckx-imap-* metadata. FetchAttachment then uses that metadata to download just the requested MIME part from the server — even in a later session. A client can therefore list messages and their attachment names cheaply and pull individual attachments only when the user actually wants them, instead of downloading every full message. (If the Email already holds the attachment bytes, they are saved without contacting the server.)Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoImap
Boolean iHeadersOnly
Boolean iUseUid
Integer iSeqNum
Variant vEmail
Handle hoEmail
Integer iNumAttach
String sFname
Handle hoSbPath
String sSavePath
String sTemp1
Boolean bTemp1
Move False To iSuccess
// Demonstrates the Imap.FetchAttachment method, which downloads one attachment from the IMAP
// server and saves it to a file. The 1st argument is the Email, the 2nd is the zero-based
// attachment index, and the 3rd is the file path to write.
//
// The compelling use case: fetch a message headers-only, inspect its attachments without
// downloading them, and then download just the attachment(s) you want.
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
// 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.
Move True To iHeadersOnly
Move False To iUseUid
Move 1 To iSeqNum
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 iHeadersOnly iSeqNum iUseUid vEmail To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
// Even though the body was not downloaded, the attachment count and names are available.
Get pvComObject of hoEmail to vEmail
Get ComGetMailNumAttach Of hoImap vEmail To iNumAttach
Showln "Attachments: " iNumAttach
If (iNumAttach > 0) Begin
// Get the attachment's original filename and use it for the saved file.
Get pvComObject of hoEmail to vEmail
Get ComGetMailAttachFilename Of hoImap vEmail 0 To sFname
Get ComLastMethodSuccess Of hoImap To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
// Build the output path "qa_output/<filename>".
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbPath
If (Not(IsComObjectCreated(hoSbPath))) Begin
Send CreateComObject of hoSbPath
End
Get ComAppend Of hoSbPath "qa_output/" To iSuccess
Get ComAppend Of hoSbPath sFname To iSuccess
Get ComGetAsString Of hoSbPath To sSavePath
// Download just the first attachment (index 0) from the server and save it to that path.
Get pvComObject of hoEmail to vEmail
Get ComFetchAttachment Of hoImap vEmail 0 sSavePath To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Saved attachment: " sSavePath
End
Get ComDisconnect Of hoImap To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoImap To sTemp1
Showln sTemp1
Procedure_Return
End
End_Procedure