Java
Java
Select an IMAP Mailbox
See more IMAP Examples
Demonstrates the Chilkat Imap.SelectMailbox method, which opens a mailbox for read-write access. A mailbox must be selected before message fetch, search, flag, copy, move, or expunge operations that act on messages. This example selects the Inbox.
Background: In IMAP, mail is organized into mailboxes (folders), and most message operations act on the currently selected one — so selecting a mailbox is the required step between logging in and working with messages.
SelectMailbox opens it read-write, allowing changes such as setting flags or deleting. When you only need to read and must not alter anything (including the \Seen flag), use ExamineMailbox instead.Chilkat Java Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
// Demonstrates the Imap.SelectMailbox method, which opens a mailbox for read-write access.
// A mailbox must be selected before message fetch, search, flag, copy, move, or expunge
// operations that act on messages.
CkImap imap = new CkImap();
imap.put_Ssl(true);
imap.put_Port(993);
success = imap.Connect("imap.example.com");
if (success == false) {
System.out.println(imap.lastErrorText());
return;
}
success = imap.Login("user@example.com","myPassword");
if (success == false) {
System.out.println(imap.lastErrorText());
return;
}
// Open the Inbox for read-write access.
success = imap.SelectMailbox("Inbox");
if (success == false) {
System.out.println(imap.lastErrorText());
return;
}
System.out.println("Inbox selected for read-write access.");
success = imap.Disconnect();
if (success == false) {
System.out.println(imap.lastErrorText());
return;
}
}
}