Java Reference
In-Depth Information
Solution
Use a
Properties
object.
Discussion
You may remember from
Storing Strings in Properties and Preferences
that
java.util.Properties
is a list of name/value pairs, and that my
FileProperties
extends
Properties
to provide loading and saving. In several places in this chapter, I use a
FileProperties
object to preload a large variety of settings, instead of hardcoding them or
having to type them all on the command line. When dealing with JavaMail, you must specify
the mail hostname, username and password, protocol to use (IMAP, POP, or mailbox for
reading), and so on. I store this information in a properties file, and most of the programs in
this chapter will use it. Here is my default file,
MailClient.properties
:
# This file contains my default Mail properties.
#
# Values for sending
Mail.address=ian@darwinsys.com
Mail.send.proto=smtp
Mail.send.host=localhost
Mail.send.debug=true
#
# Values for receiving
Mail.receive.host=localhost
Mail.receive.protocol=mbox
Mail.receive.user=*
Mail.receive.pass=*
Mail.receive.root=/var/mail/ian
The last two,
pass
and
root
, can have certain predefined values. Because nobody concerned
with security would store unencrypted passwords in a file on disk, I allow you to set
pass=*
(in uppercase), which causes some of my programs to prompt for a password. The JavaMail
API allows use of
root=INBOX
to mean the default storage location for your mail.
The keys in this list of properties intentionally begin with a capital letter because the property
names used by the JavaMail API begin with a lowercase letter. The names are rather long, so
they, too, are coded. It would be circular to encode them in a
Properties
object, though; in-