Java Reference
In-Depth Information
Next, you'll want to create an instance of the javax.mail.Authenticator class (more
properly, an instance of a concrete subclass of the abstract Authenticator class) that
can ask the user for a password. For now, we'll simply hardcode those values and pass
null instead of an actual Authenticator :
Authenticator a = null ;
We'll fill this piece in later when we discuss authentication.
Next, use these Properties and Authenticator objects to get a Session instance, like
this:
Session session = Session . getInstance ( props , a );
Ask the session for a store for the provider. Here, we want a provider for POP3:
Store store = session . getStore ( "pop3" );
Finally, you're ready to connect to the store using the connect() method. You'll need
to provide the host to connect to and the username and password to use:
store . connect ( "mail.cloud9.net" , "elharo" , "my_password" );
You can pass null for the password to indicate that the previously specified Authenti
cator should be queried for the password.
Now that the store is connected, you're ready to open a folder in the store. This step is
really more oriented to IMAP than POP, since POP servers don't keep track of different
folders. They simply provide all of a user's incoming mail as one undifferentiated amal‐
gam. For purposes of the JavaMail API, POP3 providers use the folder name INBOX:
Folder inbox = store . getFolder ( "INBOX" );
The folder is closed when you get it. You can perform some operations on a closed folder
including deleting or renaming it, but you can't get the messages out of a closed folder.
First you have to open it. You can open a folder for read access by passing the mnemonic
constant Folder.READ_WRITE to the open() method for read access, or
Folder.READ_ONLY for read/write access:
inbox . open ( Folder . READ_ONLY );
Now you're ready to retrieve the messages with the getMessages() method, which
returns an array containing pointers to all the messages in the folder:
Message [] messages = inbox . getMessages ();
This call is lazy. That is, it does not actually download the message headers and content.
That will be retrieved later when you ask each message for its data.
The Message class provides many methods for working with individual messages. It has
methods to get the various header fields of the message, get the content of the message,
reply to the message, and more. We'll discuss these soon, when we talk about the Message
Search WWH ::




Custom Search