Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nHeadersOnly
LOCAL nUseUid
LOCAL nSeqNum
LOCAL oEmail
LOCAL nNumAttach
LOCAL i
LOCAL cFname

nSuccess := 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.

oImap := CreateObject("Chilkat.Imap")

oImap:Ssl := 1
oImap:Port := 993

nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nSuccess := oImap:Login("user@example.com", "myPassword")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
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.
nHeadersOnly := 1
nUseUid := 0
nSeqNum := 1
oEmail := CreateObject("Chilkat.Email")
nSuccess := oImap:FetchEmail(nHeadersOnly, nSeqNum, nUseUid, oEmail)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oEmail:destroy()
    RETURN
ENDIF

//  Loop over the attachments and print each filename.
nNumAttach := oImap:GetMailNumAttach(oEmail)
? "Attachments: " + Str(nNumAttach)

FOR i := 0 TO nNumAttach - 1
    //  GetMailAttachFilename returns a string, so assign it to a string variable.
    cFname := oImap:GetMailAttachFilename(oEmail, i)
    IF (oImap:LastMethodSuccess == 0)
        ? oImap:LastErrorText
        oImap:destroy()
        oEmail:destroy()
        RETURN
    ENDIF

    ? cFname
NEXT

nSuccess := oImap:Disconnect()
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oEmail:destroy()
    RETURN
ENDIF

oImap:destroy()
oEmail:destroy()