Sample code for 30+ languages & platforms
Lianja

Get the Number of IMAP Attachments

See more IMAP Examples

Demonstrates the Chilkat Imap.GetMailNumAttach method, which returns the number of ordinary attachments on a message. The only argument is the Email. This example fetches only the headers and reports the attachment count.

Background: A header-only fetch downloads no attachment bodies, so the Email object's own downloaded-attachment count can be 0. GetMailNumAttach instead reads the ckx-imap-numAttach metadata, giving the true count — which is what you want when building a message list that shows whether each message has attachments.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

//  Demonstrates the Imap.GetMailNumAttach method, which returns the number of ordinary
//  attachments on a message.  The only argument is the Email.

loImap = createobject("CkImap")

loImap.Ssl = .T.
loImap.Port = 993

llSuccess = loImap.Connect("imap.example.com")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

llSuccess = loImap.Login("user@example.com","myPassword")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

llSuccess = loImap.SelectMailbox("Inbox")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    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.
llHeadersOnly = .T.
llUseUid = .F.
lnSeqNum = 1
loEmail = createobject("CkEmail")
llSuccess = loImap.FetchEmail(llHeadersOnly,lnSeqNum,llUseUid,loEmail)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loEmail
    return
endif

lnNumAttach = loImap.GetMailNumAttach(loEmail)
? "This message has " + str(lnNumAttach) + " attachment(s)."

llSuccess = loImap.Disconnect()
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loEmail
    return
endif



release loImap
release loEmail