Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Boolean iHeadersOnly
    Boolean iUseUid
    Integer iSeqNum
    Variant vEmail
    Handle hoEmail
    Integer iNumAttach
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End

    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993

    Get ComConnect Of hoImap "imap.example.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLogin Of hoImap "user@example.com" "myPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSelectMailbox Of hoImap "Inbox" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Move True To iHeadersOnly
    Move False To iUseUid
    Move 1 To iSeqNum
    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Get pvComObject of hoEmail to vEmail
    Get ComFetchEmail Of hoImap iHeadersOnly iSeqNum iUseUid vEmail To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoEmail to vEmail
    Get ComGetMailNumAttach Of hoImap vEmail To iNumAttach
    Showln "This message has " iNumAttach " attachment(s)."

    Get ComDisconnect Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure