Java Requires Chilkat v11.0.0+
Chilkat2 Java
Delete All IMAP Email
Demonstrates two ways to delete all email in a mailbox on an IMAP server.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Imap;
import com.chilkatsoft.chilkat2.MessageSet;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Imap imap = new Imap();
// Turn on session logging:
imap.setKeepSessionLog(true);
// Connect to an IMAP server.
// Use TLS
imap.setSsl(true);
imap.setPort(993);
try {
imap.connect("imap.example.com");
// Login
imap.login("myLogin", "myPassword");
// Select an IMAP mailbox
imap.selectMailbox("Inbox.RubyMail");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Get the complete set of Uids for email in the selected mailbox.
MessageSet messageSet = new MessageSet();
try {
imap.queryMbx("ALL", true, messageSet);
// Set the Deleted flag for each message.
// (ExpungeAndClose must be called to finalize the delete.)
imap.setFlags(messageSet, "Deleted", 1);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Alternatively, the Deleted flag may be set for each UID
// individiually, but this is less efficient:
int i = 0;
int n = messageSet.getCount();
while (i < n) {
try {
imap.setFlag(messageSet.getId(i), messageSet.getHasUids(), "Deleted", 1);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
i = i + 1;
}
// Expunge and close the mailbox.
try {
imap.expungeAndClose();
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Display the session log.
System.out.println(imap.getSessionLog());
// Disconnect from the IMAP server.
imap.disconnect();
}
}