Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
integer li_HeadersOnly
integer li_UseUid
integer li_SeqNum
oleobject loo_Email
integer li_NumAttach
string ls_Fname
oleobject loo_SbPath
string ls_SavePath

li_Success = 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.

loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
    destroy loo_Imap
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Imap.Ssl = 1
loo_Imap.Port = 993

li_Success = loo_Imap.Connect("imap.example.com")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.Login("user@example.com","myPassword")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.SelectMailbox("Inbox")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

//  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.
li_HeadersOnly = 1
li_UseUid = 0
li_SeqNum = 1
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

li_Success = loo_Imap.FetchEmail(li_HeadersOnly,li_SeqNum,li_UseUid,loo_Email)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Email
    return
end if

//  Even though the body was not downloaded, the attachment count and names are available.
li_NumAttach = loo_Imap.GetMailNumAttach(loo_Email)
Write-Debug "Attachments: " + string(li_NumAttach)

if li_NumAttach > 0 then
    //  Get the attachment's original filename and use it for the saved file.
    ls_Fname = loo_Imap.GetMailAttachFilename(loo_Email,0)
    if loo_Imap.LastMethodSuccess = 0 then
        Write-Debug loo_Imap.LastErrorText
        destroy loo_Imap
        destroy loo_Email
        return
    end if

    //  Build the output path "qa_output/<filename>".
    loo_SbPath = create oleobject
    li_rc = loo_SbPath.ConnectToNewObject("Chilkat.StringBuilder")

    loo_SbPath.Append("qa_output/")
    loo_SbPath.Append(ls_Fname)
    ls_SavePath = loo_SbPath.GetAsString()

    //  Download just the first attachment (index 0) from the server and save it to that path.
    li_Success = loo_Imap.FetchAttachment(loo_Email,0,ls_SavePath)
    if li_Success = 0 then
        Write-Debug loo_Imap.LastErrorText
        destroy loo_Imap
        destroy loo_Email
        destroy loo_SbPath
        return
    end if

    Write-Debug "Saved attachment: " + ls_SavePath
end if

li_Success = loo_Imap.Disconnect()
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_Email
    destroy loo_SbPath
    return
end if



destroy loo_Imap
destroy loo_Email
destroy loo_SbPath