Java Reference
In-Depth Information
7. Open the INBOX folder.
8. Open the folder you want inside the INBOX folder. Repeat as many times as nec‐
essary to reach the folder you're seeking.
9. Get the messages from the folder as an array of
Message
objects.
10. Iterate through the array of messages, processing each one in turn using the methods
of the
Message
class. For instance, you might print out each message or simply
display the sender, subject, and other vital information in a GUI for the user to
select from, as in
Figure 3-1
.
11. Close the folder.
12. Close the store.
Figure 3-1. A GUI for selecting mail messages
Each of these steps is individually quite simple. The first is to set up the properties for
the mail session. Properties you might want to set include
mail.store.protocol
,
mail.pop3.user
, and
mail.pop3.host
:
Properties
props
=
new
Properties
();
props
.
put
(
"mail.pop3.host"
,
"pop.gmail.com"
);
props
.
put
(
"mail.store.protocol"
,
"pop3"
);
props
.
put
(
"mail.pop3.user"
,
"erharold"
);
Alternatively you can provide this information in method calls, in which case an empty
Properties
object is enough. Personally I prefer using method arguments instead of
system properties when possible since it avoids unexpected, hard-to-debug global con‐
figuration problems.