Visual FoxPro
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loImap
LOCAL lnHeadersOnly
LOCAL lnUseUid
LOCAL lnSeqNum
LOCAL loEmail
LOCAL lnNumAttach
LOCAL lcFname
LOCAL loSbPath
LOCAL lcSavePath
lnSuccess = 0
* 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.
loImap = CreateObject('Chilkat.Imap')
loImap.Ssl = 1
loImap.Port = 993
lnSuccess = loImap.Connect("imap.example.com")
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
CANCEL
ENDIF
lnSuccess = loImap.Login("user@example.com","myPassword")
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
CANCEL
ENDIF
lnSuccess = loImap.SelectMailbox("Inbox")
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
CANCEL
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.
lnHeadersOnly = 1
lnUseUid = 0
lnSeqNum = 1
loEmail = CreateObject('Chilkat.Email')
lnSuccess = loImap.FetchEmail(lnHeadersOnly,lnSeqNum,lnUseUid,loEmail)
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
RELEASE loEmail
CANCEL
ENDIF
* Even though the body was not downloaded, the attachment count and names are available.
lnNumAttach = loImap.GetMailNumAttach(loEmail)
? "Attachments: " + STR(lnNumAttach)
IF (lnNumAttach > 0) THEN
* Get the attachment's original filename and use it for the saved file.
lcFname = loImap.GetMailAttachFilename(loEmail,0)
IF (loImap.LastMethodSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
RELEASE loEmail
CANCEL
ENDIF
* Build the output path "qa_output/<filename>".
loSbPath = CreateObject('Chilkat.StringBuilder')
loSbPath.Append("qa_output/")
loSbPath.Append(lcFname)
lcSavePath = loSbPath.GetAsString()
* Download just the first attachment (index 0) from the server and save it to that path.
lnSuccess = loImap.FetchAttachment(loEmail,0,lcSavePath)
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
RELEASE loEmail
RELEASE loSbPath
CANCEL
ENDIF
? "Saved attachment: " + lcSavePath
ENDIF
lnSuccess = loImap.Disconnect()
IF (lnSuccess = 0) THEN
? loImap.LastErrorText
RELEASE loImap
RELEASE loEmail
RELEASE loSbPath
CANCEL
ENDIF
RELEASE loImap
RELEASE loEmail
RELEASE loSbPath