Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
integer li_FetchUids
oleobject loo_MessageSet
oleobject loo_Bundle
integer li_HeadersOnly
oleobject loo_Email
integer i
integer j
integer li_NumEmails
integer li_NumAttach
integer li_AttachSize

li_Success = 0

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

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

// Connect to an IMAP server.
// Use TLS
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

// Login
li_Success = loo_Imap.Login("****","****")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

// Select an IMAP mailbox
li_Success = loo_Imap.SelectMailbox("Inbox")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

// Get the message IDs of all the emails in the mailbox
// We can choose to fetch UIDs or sequence numbers.
li_FetchUids = 1
loo_MessageSet = create oleobject
li_rc = loo_MessageSet.ConnectToNewObject("Chilkat.MessageSet")

li_Success = loo_Imap.QueryMbx("ALL",li_FetchUids,loo_MessageSet)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_MessageSet
    return
end if

// 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.
loo_Bundle = create oleobject
li_rc = loo_Bundle.ConnectToNewObject("Chilkat.EmailBundle")

li_HeadersOnly = 1
li_Success = loo_Imap.FetchMsgSet(li_HeadersOnly,loo_MessageSet,loo_Bundle)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_MessageSet
    destroy loo_Bundle
    return
end if

// Loop over the email objects and display information about each:
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

i = 0
j = 0
li_NumEmails = loo_Bundle.MessageCount
do while i < li_NumEmails
    loo_Bundle.EmailAt(i,loo_Email)

    // Display the From and Subject
    Write-Debug loo_Email.From
    Write-Debug loo_Email.Subject

    // Display the recipients:
    j = 0
    do while j < loo_Email.NumTo
        Write-Debug "TO: " + loo_Email.GetToName(j) + ", " + loo_Email.GetToAddr(j)
        j = j + 1
    loop
    j = 0
    do while j < loo_Email.NumCC
        Write-Debug "CC: " + loo_Email.GetCcName(j) + ", " + loo_Email.GetCcAddr(j)
        j = j + 1
    loop

    // Show the total size of the email, including body and attachments:
    Write-Debug string(loo_Email.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:
    li_NumAttach = loo_Imap.GetMailNumAttach(loo_Email)
    j = 0
    do while j < li_NumAttach
        Write-Debug loo_Imap.GetMailAttachFilename(loo_Email,j)
        li_AttachSize = loo_Imap.GetMailAttachSize(loo_Email,j)
        Write-Debug "    size = " + string(li_AttachSize) + " bytes"
        j = j + 1
    loop

    Write-Debug "--"

    i = i + 1
loop

// Disconnect from the IMAP server.
li_Success = loo_Imap.Disconnect()


destroy loo_Imap
destroy loo_MessageSet
destroy loo_Bundle
destroy loo_Email