Java Reference
In-Depth Information
Store store = session.getStore("imaps");
store.connect(host,username,password);
System.out.println(store);
Folder inbox = store.getFolder(folder);
inbox.open(Folder.READ_WRITE);
int messageCount = inbox.getMessageCount();
int startMessage = messageCount - 5;
int endMessage = messageCount;
if (messageCount < 5) startMessage =0;
Message messages[] =
inbox.getMessages(startMessage,endMessage);
for (Message message : messages) {
boolean hasBeenRead = false;
for (Flags.Flag flag
: message.getFlags().getSystemFlags()) {
if (flag == Flags.Flag.SEEN) {
hasBeenRead = true;
break;
}
}
message.setFlag(Flags.Flag.SEEN, true);
System.out.println(message.getSubject() + " "+
(hasBeenRead? "(read)" : "") + message.getContent());
}
inbox.close(true);
How It Works
A Store object allows you to access e-mail mailbox information. By creating a
Store and then requesting the Inbox folder, you gain access to the messages in the
main mailbox of your IMAP account. With the folder object, you can request to down-
load the messages from the inbox. To do so, you use the getMessages (start,
end) method. The inbox also provides a getMessageCount() method, which al-
lows you to know how many e-mails are in the inbox. Keep in mind that the messages
start at index 1.
Search WWH ::




Custom Search