Sample code for 30+ languages & platforms
Lianja

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

Lianja
llSuccess = .F.

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

loImap = createobject("CkImap")

// Connect to an IMAP server.
// Use TLS
loImap.Ssl = .T.
loImap.Port = 993
llSuccess = loImap.Connect("imap.example.com")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

// Login
llSuccess = loImap.Login("****","****")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

// Select an IMAP mailbox
llSuccess = loImap.SelectMailbox("Inbox")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

// Get the message IDs of all the emails in the mailbox
// We can choose to fetch UIDs or sequence numbers.
llFetchUids = .T.
loMessageSet = createobject("CkMessageSet")
llSuccess = loImap.QueryMbx("ALL",llFetchUids,loMessageSet)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMessageSet
    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.
loBundle = createobject("CkEmailBundle")
llHeadersOnly = .T.
llSuccess = loImap.FetchMsgSet(llHeadersOnly,loMessageSet,loBundle)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMessageSet
    release loBundle
    return
endif

// Loop over the email objects and display information about each:
loEmail = createobject("CkEmail")
i = 0
j = 0
lnNumEmails = loBundle.MessageCount
do while i < lnNumEmails
    loBundle.EmailAt(i,loEmail)

    // Display the From and Subject
    ? loEmail.From
    ? loEmail.Subject

    // Display the recipients:
    j = 0
    do while j < loEmail.NumTo
        ? "TO: " + loEmail.GetToName(j) + ", " + loEmail.GetToAddr(j)
        j = j + 1
    enddo
    j = 0
    do while j < loEmail.NumCC
        ? "CC: " + loEmail.GetCcName(j) + ", " + loEmail.GetCcAddr(j)
        j = j + 1
    enddo

    // Show the total size of the email, including body and attachments:
    ? str(loEmail.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:
    lnNumAttach = loImap.GetMailNumAttach(loEmail)
    j = 0
    do while j < lnNumAttach
        ? loImap.GetMailAttachFilename(loEmail,j)
        lnAttachSize = loImap.GetMailAttachSize(loEmail,j)
        ? "    size = " + str(lnAttachSize) + " bytes"
        j = j + 1
    enddo

    ? "--"

    i = i + 1
enddo

// Disconnect from the IMAP server.
llSuccess = loImap.Disconnect()


release loImap
release loMessageSet
release loBundle
release loEmail