Sample code for 30+ languages & platforms
DataFlex

IMAP Delete Old Email (before a specified date)

See more IMAP Examples

Demonstrates how to delete email older than a particular date.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Boolean iFetchUids
    Variant vMessageSet
    Handle hoMessageSet
    Variant vEmail
    Handle hoEmail
    Integer i
    Integer iCount
    Boolean iHeaderOnly
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

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

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

    // Connect to an IMAP server.
    // Use TLS
    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993
    // Use your IMAP server domain.  This example 
    Get ComConnect Of hoImap "imap.example.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Login
    Get ComLogin Of hoImap "myLogin" "myPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Select an IMAP mailbox
    Get ComSelectMailbox Of hoImap "Inbox" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Get message ID's for emails older than 1/1/2025
    Move True To iFetchUids
    Get Create (RefClass(cComChilkatMessageSet)) To hoMessageSet
    If (Not(IsComObjectCreated(hoMessageSet))) Begin
        Send CreateComObject of hoMessageSet
    End
    Get pvComObject of hoMessageSet to vMessageSet
    Get ComQueryMbx Of hoImap "SENTBEFORE 01-Jan-2025" iFetchUids vMessageSet To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // If desired, we can examine the Subject of each email to be deleted..
    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Move 0 To i
    Get ComCount Of hoMessageSet To iCount
    Move True To iHeaderOnly

    While (i < iCount)
        Get ComGetId Of hoMessageSet i To iTemp1
        Get pvComObject of hoEmail to vEmail
        Get ComFetchEmail Of hoImap iHeaderOnly iTemp1 True vEmail To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoImap To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get ComLocalDateStr Of hoEmail To sTemp1
        Showln sTemp1
        Get ComSubject Of hoEmail To sTemp1
        Showln sTemp1

        Move (i + 1) To i
    Loop

    // Set the Deleted flag for each message:
    Get pvComObject of hoMessageSet to vMessageSet
    Get ComSetFlags Of hoImap vMessageSet "Deleted" 1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Expunge and close the mailbox.
    Get ComExpungeAndClose Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Disconnect from the IMAP server.
    Get ComDisconnect Of hoImap To iSuccess


End_Procedure