Java Reference
In-Depth Information
Discussion
The JavaMail API is designed to be easy to use.
Store
encapsulates the information and ac-
cess methods for a particular type of mail storage; the steps for using it are listed in the side-
bar.
IAN'S BASIC STEPS: READING EMAIL USING STORE
Here is how you read email using
Store
:
1. Get a
Session
object using
Session.getDefaultInstance()
. You can pass
Sys-
tem.getProperties()
as the
Properties
argument.
2. Get a
Store
from the
Session
object.
3. Get the root
Folder
.
4. If the root
Folder
can contain subfolders, list them.
5. For each
Folder
that can contain messages, call
getMessages()
, which returns an array
of
Message
objects.
6. Do what you will with the messages (usually, display the headers and let the user select
which message to view).
Java provides a
Store
class for the IMAP and POP3 transport mechanisms. The programs in
this chapter will work more-or-less the same whether you use either of these, or a third-party
mailbox “spool file” typically found on Unix mail servers. I've tested others using the POP
and IMAP stores. The programs can be made to use any of these (assuming it's on your
classpath, of ourse) just by passing the appropriate protocol name where “pop3” appears in
the following examples. I've tested several of the programs using Sun's POP store and sever-
al POP servers (CUCIpop and PMDF).
I (try to) delete most of the email I get on one of my systems, so there were only two mes-
sages to be read when I ran my first “mailbox lister” program:
$ java email.MailLister pop3 dalai ian '*' /var/mail/ian
Password: (not shown)