Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

Imap.GetMailSize vs Email.Size

Shows how to get the total size of an email, as well as the sizes of the attachments. This can be done when either full-emails or headers-only are downloaded.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nFetchUids
LOCAL oMessageSet
LOCAL oBundle
LOCAL nHeadersOnly
LOCAL oEmail
LOCAL i
LOCAL j
LOCAL nNumEmails
LOCAL nNumAttach
LOCAL nAttachSize

nSuccess := 0

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oImap := CreateObject("Chilkat.Imap")

//  Connect to an IMAP server.
//  Use TLS
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Login
nSuccess := oImap:Login("****", "****")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Select an IMAP mailbox
nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Get the message IDs of all the emails in the mailbox
//  We can choose to fetch UIDs or sequence numbers.
nFetchUids := 1
oMessageSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("ALL", nFetchUids, oMessageSet)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessageSet:destroy()
    RETURN
ENDIF

//  When downloading headers, each email object contains
//  (obviously) the headers, but the body will be missing.
//  Attachments will not be included.  However, it is
//  possible to get information about the attachments
//  as well as the complete size of the email.
oBundle := CreateObject("Chilkat.EmailBundle")
nHeadersOnly := 1
nSuccess := oImap:FetchMsgSet(nHeadersOnly, oMessageSet, oBundle)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessageSet:destroy()
    oBundle:destroy()
    RETURN
ENDIF

//  Loop over the email objects and display information about each:
oEmail := CreateObject("Chilkat.Email")
i := 0
j := 0
nNumEmails := oBundle:MessageCount
DO WHILE i < nNumEmails
    oBundle:EmailAt(i, oEmail)

    //  Display the From and Subject
    ? oEmail:From
    ? oEmail:Subject

    //  Display the recipients:
    j := 0
    DO WHILE j < oEmail:NumTo
        ? "TO: " + oEmail:GetToName(j) + ", " + oEmail:GetToAddr(j)
        j := j + 1
    ENDDO
    j := 0
    DO WHILE j < oEmail:NumCC
        ? "CC: " + oEmail:GetCcName(j) + ", " + oEmail:GetCcAddr(j)
        j := j + 1
    ENDDO

    //  Show the total size of the email, including body and attachments:
    ? Str(oEmail:Size)

    //  When a full email is downloaded, we would use the
    //  email.NumAttachments property in conjunction with the
    //  email.GetMailAttach* methods.
    //  However, when an email object contains only the header,
    //  we need to access the attachment info differently:
    nNumAttach := oImap:GetMailNumAttach(oEmail)
    j := 0
    DO WHILE j < nNumAttach
        ? oImap:GetMailAttachFilename(oEmail, j)
        nAttachSize := oImap:GetMailAttachSize(oEmail, j)
        ? "    size = " + Str(nAttachSize) + " bytes"
        j := j + 1
    ENDDO

    ? "--"

    i := i + 1
ENDDO

//  Disconnect from the IMAP server.
nSuccess := oImap:Disconnect()

oImap:destroy()
oMessageSet:destroy()
oBundle:destroy()
oEmail:destroy()