Java Reference
In-Depth Information
(essentially the subject and addressees of the message), the flags of the message, or the
content info of the messages. The three groups you can ask for are given as constant
FetchProfile.Item
objects. They are
FetchProfile.Item.ENVELOPE
,
FetchPro
file.Item.FLAGS
, and
FetchProfile.Item.CONTENT_INFO
.
The
FetchProfile
class has a simple noargs constructor as well as methods for con‐
structing a new profile, adding particular items and headers to the profile, and testing
whether a particular item is part of a particular profile:
public
FetchProfile
()
public
void
add
(
FetchProfile
.
Item
item
)
public
void
add
(
String
headerName
)
public
boolean
contains
(
FetchProfile
.
Item
item
)
public
boolean
contains
(
String
headerName
)
public
FetchProfile
.
Item
[]
getItems
()
public
String
[]
getHeaderNames
()
For example, suppose you wanted to download just the subjects, the To: addresses, and
the content information of a block of messages. Fetch them like this:
Message
[]
messages
=
folder
.
getMessages
();
FetchProfile
fp
=
new
FetchProfile
();
fp
.
add
(
FetchProfile
.
Item
.
CONTENT_INFO
);
fp
.
add
(
"Subject"
);
fp
.
add
(
"To"
);
folder
.
fetch
(
messages
,
fp
);
Searching Folders
If the server supports searching (as many IMAP servers do and most POP servers don't),
it's easy to search a folder for the messages meeting certain criteria. The criteria are
encoded in
SearchTerm
objects:
public
abstract
class
SearchTerm
extends
Object
The
SearchTerm
class is abstract, but the JavaMail API provides many subclasses for
performing common searches:
public
abstract
class
AddressTerm
extends
SearchTerm
public
abstract
class
FlagTerm
extends
SearchTerm
public
abstract
class
StringTerm
extends
SearchTerm
public
final
class
FromTerm
extends
AddressTerm
public
final
class
FromStringTerm
extends
AddressStringTerm
public
final
class
RecipientTerm
extends
AddressTerm
public
final
class
AddressStringTerm
extends
StringTerm
public
final
class
BodyTerm
extends
StringTerm
public
final
class
HeaderTerm
extends
StringTerm
public
final
class
MessageIDTerm
extends
StringTerm
public
final
class
SubjectTerm
extends
StringTerm
public
abstract
class
DateTerm
extends
ComparisonTerm