Sample code for 30+ languages & platforms
PureBasic

Fetch Only an IMAP Message's Headers

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchSingleHeaderAsMime method, which downloads and returns the MIME header block for one message, without downloading the body. If the second argument (bUID) is true, the first argument is a UID; otherwise it is a sequence number. This example fetches message 1's headers.

Background: Headers are tiny compared to a full message with attachments, so fetching just the header block is the fast, bandwidth-friendly way to build a message list — sender, subject, date — without pulling every body. It is also gentler on read state: retrieving only headers does not mark a message as seen. Download the full message later, on demand, when the user opens it.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkImap.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Imap.FetchSingleHeaderAsMime method, which downloads and returns the MIME
    ;  header block for one message, without downloading the body.  If the 2nd argument (bUID)
    ;  is 1, the 1st argument is a UID; otherwise it is a sequence number.

    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)

    success = CkImap::ckConnect(imap,"imap.example.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,"user@example.com","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckSelectMailbox(imap,"Inbox")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ;  Download only the MIME header block of message sequence number 1.  bUID = 0.
    headers.s = CkImap::ckFetchSingleHeaderAsMime(imap,1,0)
    If CkImap::ckLastMethodSuccess(imap) = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    Debug headers

    success = CkImap::ckDisconnect(imap)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf



    CkImap::ckDispose(imap)


    ProcedureReturn
EndProcedure