Top Description Fields Methods
javax.mail

public Interface Part

Known Direct Subinterfaces
javax.mail.internet.MimePart
Known Direct Implementers
javax.mail.BodyPart, javax.mail.Message
Imports
java.io.*, java.util.Enumeration, javax.activation.DataHandler

The Part interface is the common base interface for Messages and BodyParts.

Part consists of a set of attributes and a "Content".

Attributes:

The JavaMail API defines a set of standard Part attributes that are considered to be common to most existing Mail systems. These attributes have their own settor and gettor methods. Mail systems may support other Part attributes as well, these are represented as name-value pairs where both the name and value are Strings.

Content:

The data type of the "content" is returned by the getContentType() method. The MIME typing system is used to name data types.

The "content" of a Part is available in various formats:

Part provides the writeTo() method that streams out its bytestream in mail-safe form suitable for transmission. This bytestream is typically an aggregation of the Part attributes and its content's bytestream.

Message and BodyPart implement the Part interface. Note that in MIME parlance, Part models an Entity (RFC 2045, Section 2.4).

Author
John Mani

Field Summary

Modifier and TypeField and Description
public static final String
ATTACHMENT

This part should be presented as an attachment.

public static final String
INLINE

This part should be presented inline.

Method Summary

Modifier and TypeMethod and Description
public void
addHeader(String
the name of this header
header_name
,
String
the value for this header
header_value
)

Add this value to the existing values for this header_name.

public Enumeration<Header>

Returns:

enumeration of Header objects
getAllHeaders
()

Return all the headers from this part as an Enumeration of Header objects.

public Object

Returns:

Object
getContent
()

Return the content as a Java object.

public String

Returns:

The ContentType of this part
getContentType
()

Returns the Content-Type of the content of this part.

public DataHandler

Returns:

DataHandler for the content
getDataHandler
()

Return a DataHandler for the content within this part.

public String

Returns:

description of this part
getDescription
()

Return a description String for this part.

public String

Returns:

disposition of this part, or null if unknown
getDisposition
()

Return the disposition of this part.

public String

Returns:

Filename to associate with this part
getFileName
()

Get the filename associated with this part, if possible.

public String[]

Returns:

the value fields for all headers with this name
getHeader
(String
the name of this header
header_name
)

Get all the headers for this header name.

public InputStream

Returns:

an InputStream
getInputStream
()

Return an input stream for this part's "content".

public int

Returns:

number of lines in the content.
getLineCount
()

Return the number of lines in the content of this part.

public Enumeration<Header>

Returns:

enumeration of Header objects
getMatchingHeaders
(String[]
the headers to match
header_names
)

Return matching headers from this part as an Enumeration of Header objects.

public Enumeration<Header>

Returns:

enumeration of Header objects
getNonMatchingHeaders
(String[]
the headers to not match
header_names
)

Return non-matching headers from this envelope as an Enumeration of Header objects.

public int

Returns:

size of content in bytes
getSize
()

Return the size of the content of this part in bytes.

public boolean

Returns:

true if this part is of the specified type
isMimeType
(String
the MIME type to test
mimeType
)

Is this Part of the specified MIME type?

public void
removeHeader(String
the name of this header
header_name
)

Remove all headers with this name.

public void
setContent(Object
A java object.
obj
,
String
MIME type of this object.
type
)

A convenience method for setting this part's content.

public void
setContent(Multipart
The multipart object that is the Message's content
mp
)

This method sets the given Multipart object as this message's content.

public void
setDataHandler(DataHandler
The DataHandler for the content.
dh
)

This method provides the mechanism to set this part's content.

public void
setDescription(String
description of this part
description
)

Set a description String for this part.

public void
setDisposition(String
disposition of this part
disposition
)

Set the disposition of this part.

public void
setFileName(String
Filename to associate with this part
filename
)

Set the filename associated with this part, if possible.

public void
setHeader(String
the name of this header
header_name
,
String
the value for this header
header_value
)

Set the value for this header_name.

public void
setText(String
The text that is the Message's content.
text
)

A convenience method that sets the given String as this part's content with a MIME type of "text/plain".

public void
writeTo(OutputStream
the stream to write to
os
)

Output a bytestream for this Part.

Field Detail

ATTACHMENTback to summary
public static final String ATTACHMENT

This part should be presented as an attachment.

See Also
getDisposition, setDisposition
INLINEback to summary
public static final String INLINE

This part should be presented inline.

See Also
getDisposition, setDisposition

Method Detail

addHeaderback to summary
public void addHeader(String header_name, String header_value) throws MessagingException

Add this value to the existing values for this header_name.

Parameters
header_name:String

the name of this header

header_value:String

the value for this header

Exceptions
MessagingException:
for other failures
IllegalWriteException:
if the underlying implementation does not support modification of existing values
IllegalStateException:
if this Part is obtained from a READ_ONLY folder
getAllHeadersback to summary
public Enumeration<Header> getAllHeaders() throws MessagingException

Return all the headers from this part as an Enumeration of Header objects.

Returns:Enumeration<Header>

enumeration of Header objects

Exceptions
MessagingException:
for failures
getContentback to summary
public Object getContent() throws IOException, MessagingException

Return the content as a Java object. The type of the returned object is of course dependent on the content itself. For example, 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 DataHandler system, an input stream is returned as the content

This is a convenience method that just invokes the DataHandler's getContent() method

Returns:Object

Object

Exceptions
IOException:
this is typically thrown by the DataHandler. Refer to the documentation for javax.activation.DataHandler for more details.
MessagingException:
for other failures
See Also
javax.activation.DataHandler#getContent
getContentTypeback to summary
public String getContentType() throws MessagingException

Returns the Content-Type of the content of this part. Returns null if the Content-Type could not be determined.

The MIME typing system is used to name Content-types.

Returns:String

The ContentType of this part

Exceptions
MessagingException:
for failures
See Also
javax.activation.DataHandler
getDataHandlerback to summary
public DataHandler getDataHandler() throws MessagingException

Return a DataHandler for the content within this part. The DataHandler allows clients to operate on as well as retrieve the content.

Returns:DataHandler

DataHandler for the content

Exceptions
MessagingException:
for failures
getDescriptionback to summary
public String getDescription() throws MessagingException

Return a description String for this part. This typically associates some descriptive information with this part. Returns null if none is available.

Returns:String

description of this part

Exceptions
MessagingException:
for failures
getDispositionback to summary
public String getDisposition() throws MessagingException

Return the disposition of this part. The disposition describes how the part should be presented to the user. (See RFC 2183.) The return value should be considered without regard to case. For example:

 String disp = part.getDisposition();
 if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT))
	// treat as attachment if not first part
 
Returns:String

disposition of this part, or null if unknown

Exceptions
MessagingException:
for failures
See Also
ATTACHMENT, INLINE, getFileName
getFileNameback to summary
public String getFileName() throws MessagingException

Get the filename associated with this part, if possible. Useful if this part represents an "attachment" that was loaded from a file. The filename will usually be a simple name, not including directory components.

Returns:String

Filename to associate with this part

Exceptions
MessagingException:
for failures
getHeaderback to summary
public String[] getHeader(String header_name) throws MessagingException

Get all the headers for this header name. Returns null if no headers for this header name are available.

Parameters
header_name:String

the name of this header

Returns:String[]

the value fields for all headers with this name

Exceptions
MessagingException:
for failures
getInputStreamback to summary
public InputStream getInputStream() throws IOException, MessagingException

Return an input stream for this part's "content". Any mail-specific transfer encodings will be decoded before the input stream is provided.

This is typically a convenience method that just invokes the DataHandler's getInputStream() method.

Returns:InputStream

an InputStream

Exceptions
IOException:
this is typically thrown by the DataHandler. Refer to the documentation for javax.activation.DataHandler for more details.
MessagingException:
for other failures
See Also
getDataHandler, javax.activation.DataHandler#getInputStream
getLineCountback to summary
public int getLineCount() throws MessagingException

Return the number of lines in the content of this part. Return -1 if the number cannot be determined. Note that this number may not be an exact measure of the content length and may or may not account for any transfer encoding of the content.

Returns:int

number of lines in the content.

Exceptions
MessagingException:
for failures
getMatchingHeadersback to summary
public Enumeration<Header> getMatchingHeaders(String[] header_names) throws MessagingException

Return matching headers from this part as an Enumeration of Header objects.

Parameters
header_names:String[]

the headers to match

Returns:Enumeration<Header>

enumeration of Header objects

Exceptions
MessagingException:
for failures
getNonMatchingHeadersback to summary
public Enumeration<Header> getNonMatchingHeaders(String[] header_names) throws MessagingException

Return non-matching headers from this envelope as an Enumeration of Header objects.

Parameters
header_names:String[]

the headers to not match

Returns:Enumeration<Header>

enumeration of Header objects

Exceptions
MessagingException:
for failures
getSizeback to summary
public int getSize() throws MessagingException

Return the size of the content of this part in bytes. Return -1 if the size cannot be determined.

Note that the size may not be an exact measure of the content size and may or may not account for any transfer encoding of the content. The size is appropriate for display in a user interface to give the user a rough idea of the size of this part.

Returns:int

size of content in bytes

Exceptions
MessagingException:
for failures
isMimeTypeback to summary
public boolean isMimeType(String mimeType) throws MessagingException

Is this Part of the specified MIME type? This method compares only the primaryType and subType. The parameters of the content types are ignored.

For example, this method will return true when comparing a Part of content type "text/plain" with "text/plain; charset=foobar".

If the subType of mimeType is the special character '*', then the subtype is ignored during the comparison.

Parameters
mimeType:String

the MIME type to test

Returns:boolean

true if this part is of the specified type

Exceptions
MessagingException:
for failures
removeHeaderback to summary
public void removeHeader(String header_name) throws MessagingException

Remove all headers with this name.

Parameters
header_name:String

the name of this header

Exceptions
MessagingException:
for other failures
IllegalWriteException:
if the underlying implementation does not support modification of existing values
IllegalStateException:
if this Part is obtained from a READ_ONLY folder
setContentback to summary
public void setContent(Object obj, String type) throws MessagingException

A convenience method for setting this part's content. The part internally wraps the content in a DataHandler.

Note that a DataContentHandler class for the specified type should be available to the JavaMail implementation for this to work right. i.e., to do setContent(foobar, "application/x-foobar"), a DataContentHandler for "application/x-foobar" should be installed. Refer to the Java Activation Framework for more information.

Parameters
obj:Object

A java object.

type:String

MIME type of this object.

Exceptions
MessagingException:
for other failures
IllegalWriteException:
if the underlying implementation does not support modification of existing values
IllegalStateException:
if this Part is obtained from a READ_ONLY folder
setContentback to summary
public void setContent(Multipart mp) throws MessagingException

This method sets the given Multipart object as this message's content.

Parameters
mp:Multipart

The multipart object that is the Message's content

Exceptions
MessagingException:
for other failures
IllegalWriteException:
if the underlying implementation does not support modification of existing values
IllegalStateException:
if this Part is obtained from a READ_ONLY folder
setDataHandlerback to summary
public void setDataHandler(DataHandler dh) throws MessagingException

This method provides the mechanism to set this part's content. The DataHandler wraps around the actual content.

Parameters
dh:DataHandler

The DataHandler for the content.

Exceptions
MessagingException:
for other failures
IllegalWriteException:
if the underlying implementation does not support modification of existing values
IllegalStateException:
if this Part is obtained from a READ_ONLY folder
setDescriptionback to summary
public void setDescription(String description) throws MessagingException

Set a description String for this part. This typically associates some descriptive information with this part.

Parameters
description:String

description of this part

Exceptions
MessagingException:
for other failures
IllegalWriteException:
if the underlying implementation does not support modification of this header
IllegalStateException:
if this Part is obtained from a READ_ONLY folder
setDispositionback to summary
public void setDisposition(String disposition) throws MessagingException

Set the disposition of this part.

Parameters
disposition:String

disposition of this part

Exceptions
MessagingException:
for other failures
IllegalWriteException:
if the underlying implementation does not support modification of this header
IllegalStateException:
if this Part is obtained from a READ_ONLY folder
See Also
ATTACHMENT, INLINE, setFileName
setFileNameback to summary
public void setFileName(String filename) throws MessagingException

Set the filename associated with this part, if possible. Useful if this part represents an "attachment" that was loaded from a file. The filename will usually be a simple name, not including directory components.

Parameters
filename:String

Filename to associate with this part

Exceptions
MessagingException:
for other failures
IllegalWriteException:
if the underlying implementation does not support modification of this header
IllegalStateException:
if this Part is obtained from a READ_ONLY folder
setHeaderback to summary
public void setHeader(String header_name, String header_value) throws MessagingException

Set the value for this header_name. Replaces all existing header values with this new value.

Parameters
header_name:String

the name of this header

header_value:String

the value for this header

Exceptions
MessagingException:
for other failures
IllegalWriteException:
if the underlying implementation does not support modification of existing values
IllegalStateException:
if this Part is obtained from a READ_ONLY folder
setTextback to summary
public void setText(String text) throws MessagingException

A convenience method that sets the given String as this part's content with a MIME type of "text/plain".

Parameters
text:String

The text that is the Message's content.

Exceptions
MessagingException:
for other failures
IllegalWriteException:
if the underlying implementation does not support modification of existing values
IllegalStateException:
if this Part is obtained from a READ_ONLY folder
writeToback to summary
public void writeTo(OutputStream os) throws IOException, MessagingException

Output a bytestream for this Part. This bytestream is typically an aggregration of the Part attributes and an appropriately encoded bytestream from its 'content'.

Classes that implement the Part interface decide on the appropriate encoding algorithm to be used.

The bytestream is typically used for sending.

Parameters
os:OutputStream

the stream to write to

Exceptions
IOException:
if an error occurs writing to the stream or if an error is generated by the javax.activation layer.
MessagingException:
if an error occurs fetching the data to be written
See Also
javax.activation.DataHandler#writeTo