Java Reference
In-Depth Information
The
getFlags()
method returns the flags of a particular message:
public
abstract
Flags
getFlags
()
throws
MessagingException
The
isSet()
method tests whether a specified flag is set for the given message:
public
boolean
isSet
(
Flags
.
Flag
flag
)
throws
MessagingException
Finally, the
setFlags()
and
setFlag()
methods set or unset (depending on the second
argument) the flag indicated by the first argument:
public
abstract
void
setFlags
(
Flags
flag
,
boolean
set
)
throws
MessagingException
,
IllegalWriteException
,
IllegalStateException
public
void
setFlag
(
Flags
.
Flag
flag
,
boolean
set
)
throws
MessagingException
,
IllegalWriteException
,
IllegalStateException
You delete messages by setting their
Flags.Flag.DELETED
flag to
true
. For example, to
delete
message
:
message
.
setFlag
(
Flags
.
Flag
.
DELETED
,
true
);
This only marks the message as deleted. It does not actually expunge it from the file on
the server. Until the message is expunged, it can still be undeleted by setting
Flags.Flag.DELETED
back to
false
.
Example 7-2
is a slight modification of
Example 7-1
,
HeaderClient
, which prints the
flags as well. As a general rule, POP servers won't report flags. Only a protocol that stores
messages and forwards them, such as IMAP or mbox, will report flags.
Example 7-2. A program to read mailbox flags
import
javax.mail.*
;
import
javax.mail.internet.*
;
import
java.util.*
;
public
class
FlagsClient
{
public
static
void
main
(
String
[]
args
)
{
if
(
args
.
length
==
0
)
{
System
.
err
.
println
(
"Usage: java FlagsClient protocol://username@host/foldername"
);
return
;
}
URLName
server
=
new
URLName
(
args
[
0
]);
try
{
Session
session
=
Session
.
getInstance
(
new
Properties
(),
new
MailAuthenticator
(
server
.
getUsername
()));
// Connect to the server and open the folder
Folder
folder
=
session
.
getFolder
(
server
);