Sample code for 30+ languages & platforms
Xbase++

Get IMAP Attachment Sizes

See more IMAP Examples

Demonstrates the Chilkat Imap.GetMailAttachSize method, which returns the size in bytes of an attachment. The first argument is the Email and the second is the zero-based attachment index. This example fetches headers only and prints each attachment's name and size.

Background: Like the attachment filename, the size comes from ckx-imap-* metadata and is available from a header-only fetch without downloading the body. A client can therefore show attachment sizes in a message preview, and decide whether to auto-download or prompt before pulling a large file.

Chilkat Xbase++ Downloads

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

nSuccess := 0

//  Demonstrates the Imap.GetMailAttachSize method, which returns the size in bytes 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

nNumAttach := oImap:GetMailNumAttach(oEmail)

FOR i := 0 TO nNumAttach - 1
    cFname := oImap:GetMailAttachFilename(oEmail, i)
    IF (oImap:LastMethodSuccess == 0)
        ? oImap:LastErrorText
        oImap:destroy()
        oEmail:destroy()
        RETURN
    ENDIF

    nSz := oImap:GetMailAttachSize(oEmail, i)
    ? cFname + ": " + Str(nSz) + " bytes"
NEXT

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

oImap:destroy()
oEmail:destroy()