Sample code for 30+ languages & platforms
PowerBuilder

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 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
integer i
string ls_Fname

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

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

//  Loop over the attachments and print each filename.
li_NumAttach = loo_Imap.GetMailNumAttach(loo_Email)
Write-Debug "Attachments: " + string(li_NumAttach)

for i = 0 to li_NumAttach - 1
    //  GetMailAttachFilename returns a string, so assign it to a string variable.
    ls_Fname = loo_Imap.GetMailAttachFilename(loo_Email,i)
    if loo_Imap.LastMethodSuccess = 0 then
        Write-Debug loo_Imap.LastErrorText
        destroy loo_Imap
        destroy loo_Email
        return
    end if

    Write-Debug ls_Fname
next

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



destroy loo_Imap
destroy loo_Email