A part consists of a set of attributes and a content. Some of the
attributes provide metadata describing the content and its encoding,
others may describe how to process the part.
The Part interface is the common base interface for Messages and BodyParts.
The content of a part is available in various forms:
- As a data handler, using the
getDataHandler
method.
- As an input stream, using the
getInputStream
method.
- As a Java object, using the
getContent
method.
The
writeTo
method can be used to write the part to a
byte stream in mail-safe form suitable for transmission.
In MIME terms, Part models an Entity (RFC 2045, Section 2.4).
addHeader
public void addHeader(String name,
String value)
throws MessagingException
Adds the specified value to the existing values for this header name.
name
- the header namevalue
- the value to add
getAllHeaders
public Enumeration getAllHeaders()
throws MessagingException
Returns all the headers from this part.
getContent
public Object getContent()
throws IOException,
MessagingException
Returns the content of this part as a Java object.
The type of the returned object is of course dependent on the content
itself. For instance, the object returned for "text/plain" content
is usually a String object. The object returned for a "multipart"
content is always a Multipart subclass. For content-types that are
unknown to the data handler system, an input stream is returned.
getContentType
public String getContentType()
throws MessagingException
Returns the content-type of the content of this part, or
null
if the content-type could not be determined.
The MIME typing system is used to name content-types.
getDataHandler
public DataHandler getDataHandler()
throws MessagingException
Returns a data handler for the content of this part.
getDescription
public String getDescription()
throws MessagingException
Returns the description of this part.
getDisposition
public String getDisposition()
throws MessagingException
Returns the disposition of this part.
The disposition describes how the part should be presented to the user
(see RFC 2183).
Return values are not case sensitive.
getFileName
public String getFileName()
throws MessagingException
Returns the filename associated with this part, if available.
getHeader
public String[] getHeader(String name)
throws MessagingException
Returns all the values for the specified header name, or
null
if no such headers are available.
getInputStream
public InputStream getInputStream()
throws IOException,
MessagingException
Returns an input stream for reading the content of this part.
Any mail-specific transfer encodings will be decoded by the
implementation.
getLineCount
public int getLineCount()
throws MessagingException
Returns the number of lines in the content of this part, or -1 if the
number cannot be determined.
Note that this number may not be an exact measure.
getMatchingHeaders
public Enumeration getMatchingHeaders(String[] names)
throws MessagingException
Returns the matching headers from this part.
names
- the header names to match
getNonMatchingHeaders
public Enumeration getNonMatchingHeaders(String[] names)
throws MessagingException
Returns the non-matching headers from this part.
names
- the header names to ignore
getSize
public int getSize()
throws MessagingException
Returns the size of the content of this part in bytes, or -1 if the
size cannot be determined.
Note that the size may not be an exact measure of the content size,
but will be suitable for display in a user interface to give the
user an idea of the size of this part.
isMimeType
public boolean isMimeType(String mimeType)
throws MessagingException
Is this part of the specified MIME type?
This method compares only the primary type and subtype.
The parameters of the content types are ignored.
If the subtype of
mimeType
is the special character '*',
then the subtype is ignored during the comparison.
removeHeader
public void removeHeader(String name)
throws MessagingException
Removes all headers of the specified name.
setContent
public void setContent(Object obj,
String type)
throws MessagingException
Sets the content of this part using the specified object. The type of
the supplied argument must be known to the data handler system.
obj
- a Java objecttype
- the MIME content-type of this object
setContent
public void setContent(Multipart mp)
throws MessagingException
Sets the multipart content of this part.
mp
- the multipart content
setDataHandler
public void setDataHandler(DataHandler dh)
throws MessagingException
Sets the content of this part using the specified data handler.
dh
- the data handler for the content
setDescription
public void setDescription(String description)
throws MessagingException
Sets the description of this part.
description
- the description of this part
setDisposition
public void setDisposition(String disposition)
throws MessagingException
Sets the disposition of this part.
disposition
- the disposition of this part
setFileName
public void setFileName(String filename)
throws MessagingException
Sets the filename associated with this part.
filename
- the filename to associate with this part
setHeader
public void setHeader(String name,
String value)
throws MessagingException
Sets the value for the specified header name.
name
- the header namevalue
- the new value
setText
public void setText(String text)
throws MessagingException
Sets the textual content of this part, using a MIME type of
text/plain
.
text
- the textual content
writeTo
public void writeTo(OutputStream os)
throws IOException,
MessagingException
Writes this part to the specified byte stream.