Sample code for 30+ languages & platforms
Swift

Delete IMAP Email

To delete an email, the "Deleted" flag must be set to 1 for each message to be deleted. You must also call Expunge or ExpungeAndClose to remove the emails marked as deleted.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    // This example deletes email matching a criteria from Inbox.
    let imap = CkoImap()!

    // Turn on session logging:
    imap.keepSessionLog = true

    // Connect to an IMAP server.
    // Use TLS
    imap.ssl = true
    imap.port = 993
    success = imap.connect(hostname: "imap.example.com")
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    // Login
    success = imap.login(login: "myLogin", password: "myPassword")
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    // Select an IMAP mailbox
    success = imap.selectMailbox(mailbox: "Inbox")
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    var fetchUids: Bool = true
    // Get the message IDs for all emails having "FTP2" in the subject.
    let messageSet = CkoMessageSet()!
    success = imap.queryMbx(criteria: "SUBJECT FTP2", bUid: fetchUids, msgSet: messageSet)
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    // Set the Deleted flag for each message:
    success = imap.setFlags(messageSet: messageSet, flagName: "Deleted", value: 1)
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    // Expunge and close the mailbox.
    success = imap.expungeAndClose()
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    // Display the session log.
    print("\(imap.sessionLog!)")

    // Disconnect from the IMAP server.
    success = imap.disconnect()

}