Java Reference
In-Depth Information
public
void
addHeader
(
String
name
,
String
value
)
throws
MessagingException
,
IllegalWriteException
,
IllegalStateException
The
removeHeader()
method deletes all instances of the named header from this
Part
:
public
void
removeHeader
(
String
name
)
throws
MessagingException
,
IllegalWriteException
,
IllegalStateException
The
getAllHeaders()
method returns a
java.util.Enumeration
object containing all
the headers in this message:
public
Enumeration
getAllHeaders
()
throws
MessagingException
This method is annoyingly non-generic, and you'll want to suppress generics warnings.
The
Enumeration
contains one
javax.mail.Header
object for each header in the mes‐
sage:
public
class
Header
extends
Object
The
Header
class is very simple, with just a constructor to set the name and value of the
header, and
getName()
and
getValue()
methods to return them:
public
Header
(
String
name
,
String
value
)
public
String
getName
()
public
String
getValue
()
Finally, the
getMatchingHeaders()
method returns an
Enumeration
containing all the
headers in this message with names that are one of the strings in the argument
names
array. The
getNonMatchingHeaders()
method returns an
Enumeration
containing all
the headers in this message with names that are
not
one of the strings in the argument
names
array. Again, the
Enumeration
contains
Header
objects:
public
Enumeration
getMatchingHeaders
(
String
[]
names
)
throws
MessagingException
public
Enumeration
getNonMatchingHeaders
(
String
[]
names
)
throws
MessagingException
Again, these methods are not genericized so you'll probably want to suppress generics
warnings when invoking these methods.
You may recall that
Example 7-1
,
HeaderClient
, printed only a few prespecified headers,
such as To: and From:. With the methods of the
Part
interface (that
Message
imple‐
ments), it's easy to expand this to cover all headers in the message, whether known in
advance or not.
Example 7-4
demonstrates. This ability is important because email can
contain arbitrary headers; it's not limited to just a few headers mentioned in the relevant
RFCs. For instance, some graphical mail clients for X Windows use a completely non‐
standard X-Face: header, whose value is a 48-pixel by 48-pixel, black-and-white,
uuencoded bitmap of the sender's countenance. Other clients use custom headers for
purposes both more and less serious.