Sample code for 30+ languages & platforms
Classic ASP

Delete Email Individually (One at a time) from an IMAP Mailbox

Downloads email from an IMAP mailbox and deletes emails individually (one by one).

Chilkat Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

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

set imap = Server.CreateObject("Chilkat.Imap")

' Connect to an IMAP server.
' Use TLS
imap.Ssl = 1
imap.Port = 993
success = imap.Connect("imap.example.com")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

' Login
success = imap.Login("myLogin","myPassword")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

' Select an IMAP mailbox
success = imap.SelectMailbox("Inbox")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

' We can choose to fetch UIDs or sequence numbers.
fetchUids = 1

' Get the message IDs of all the emails in the mailbox
set messageSet = Server.CreateObject("Chilkat.MessageSet")
success = imap.QueryMbx("ALL",fetchUids,messageSet)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

' Fetch the emails into a bundle object:
set bundle = Server.CreateObject("Chilkat.EmailBundle")
headersOnly = 0
success = imap.FetchMsgSet(headersOnly,messageSet,bundle)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

' To mark a complete set of emails for deletion, call SetFlags:
success = imap.SetFlags(messageSet,"Deleted",1)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

' Messages can also be marked for deletion individually:
' Loop over the bundle and mark each message for deletion.
set email = Server.CreateObject("Chilkat.Email")
i = 0
numEmails = bundle.MessageCount
Do While i < numEmails
    success = bundle.EmailAt(i,email)

    Response.Write "<pre>" & Server.HTMLEncode( email.From) & "</pre>"
    Response.Write "<pre>" & Server.HTMLEncode( email.Subject) & "</pre>"

    ' To delete this email, set the "Deleted" flag to 1.
    ' The email is not actually deleted until Expunge or
    ' ExpungeAndClose is called.
    success = imap.SetMailFlag(email,"Deleted",1)
    If (success = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
        Response.End
    End If

    Response.Write "<pre>" & Server.HTMLEncode( "--") & "</pre>"

    i = i + 1
Loop

success = imap.ExpungeAndClose()
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

' Disconnect from the IMAP server.
success = imap.Disconnect()

%>
</body>
</html>