Top Description Inners Fields Constructors Methods
javax.mail.internet

public Class MimeBodyPart

extends BodyPart
implements MimePart
Class Inheritance
All Implemented Interfaces
javax.mail.internet.MimePart, javax.mail.Part
Known Direct Subclasses
javax.mail.internet.PreencodedMimeBodyPart, com.sun.mail.imap.IMAPBodyPart
Imports
javax.mail.*, javax.activation.*, java.io.*, java.util.*, com.sun.mail.util.PropUtil, .ASCIIUtility, .MimeUtil, .MessageRemovedIOException, .FolderClosedIOException, .LineOutputStream

This class represents a MIME body part. It implements the BodyPart abstract class and the MimePart interface. MimeBodyParts are contained in MimeMultipart objects.

MimeBodyPart uses the InternetHeaders class to parse and store the headers of that body part.


A note on RFC 822 and MIME headers

RFC 822 header fields must contain only US-ASCII characters. MIME allows non ASCII characters to be present in certain portions of certain headers, by encoding those characters. RFC 2047 specifies the rules for doing this. The MimeUtility class provided in this package can be used to to achieve this. Callers of the setHeader, addHeader, and addHeaderLine methods are responsible for enforcing the MIME requirements for the specified headers. In addition, these header fields must be folded (wrapped) before being sent if they exceed the line length limitation for the transport (1000 bytes for SMTP). Received headers may have been folded. The application is responsible for folding and unfolding headers as appropriate.

Authors
John Mani, Bill Shannon, Kanwar Oberoi
See Also
javax.mail.Part, javax.mail.internet.MimePart, javax.mail.internet.MimeUtility

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
MimeBodyPart.EncodedFileDataSource

A FileDataSource class that allows us to specify the Content-Type and Content-Transfer-Encoding.

pack-priv static class
MimeBodyPart.MimePartDataHandler

A special DataHandler used only as a marker to indicate that the source of the data is a MimePart (that is, a byte array or a stream).

Field Summary

Modifier and TypeField and Description
private static final boolean
protected Object
cachedContent

If our content is a Multipart of Message object, we save it the first time it's created by parsing a stream so that changes to the contained objects will not be lost.

pack-priv static final boolean
protected byte[]
content

Byte array that holds the bytes of the content of this Part.

protected InputStream
contentStream

If the data for this body part was supplied by an InputStream that implements the SharedInputStream interface, contentStream is another such stream representing the content of this body part.

private static final boolean
protected DataHandler
dh

The DataHandler object representing this Part's content.

private static final boolean
protected InternetHeaders
headers

The InternetHeaders object that stores all the headers of this body part.

private static final boolean
private static final boolean
private static final boolean
Inherited from javax.mail.BodyPart:
parent

Constructor Summary

AccessConstructor and Description
public
MimeBodyPart()

An empty MimeBodyPart object is created.

public
MimeBodyPart(InputStream
the body part Input Stream
is
)

Constructs a MimeBodyPart by reading and parsing the data from the specified input stream.

public
MimeBodyPart(InternetHeaders
The header of this part
headers
,
byte[]
bytes representing the body of this part.
content
)

Constructs a MimeBodyPart using the given header and content bytes.

Method Summary

Modifier and TypeMethod and Description
public void
addHeader(String
header name
name
,
String
header value
value
)

Implements javax.mail.Part.addHeader.

Add this value to the existing values for this header_name.
public void
addHeaderLine(String
the line to add
line
)

Implements javax.mail.internet.MimePart.addHeaderLine.

Add a header line to this body part
public void
attachFile(File
the File object to attach
file
)

Use the specified file to provide the data for this part.

public void
attachFile(String
the name of the file to attach
file
)

Use the specified file to provide the data for this part.

public void
attachFile(File
the File object to attach
file
,
String
the Content-Type, or null
contentType
,
String
the Content-Transfer-Encoding, or null
encoding
)

Use the specified file with the specified Content-Type and Content-Transfer-Encoding to provide the data for this part.

public void
attachFile(String
the name of the file
file
,
String
the Content-Type, or null
contentType
,
String
the Content-Transfer-Encoding, or null
encoding
)

Use the specified file with the specified Content-Type and Content-Transfer-Encoding to provide the data for this part.

public Enumeration<String>
getAllHeaderLines()

Implements javax.mail.internet.MimePart.getAllHeaderLines.

Get all header lines as an Enumeration of Strings.
public Enumeration<Header>
getAllHeaders()

Implements javax.mail.Part.getAllHeaders.

Return all the headers from this Message as an Enumeration of Header objects.
public Object

Returns:

Object
getContent
()

Implements javax.mail.Part.getContent.

Return the content as a Java object.
public String
getContentID()

Implements javax.mail.internet.MimePart.getContentID.

Returns the value of the "Content-ID" header field.
public String[]
getContentLanguage()

Implements javax.mail.internet.MimePart.getContentLanguage.

Get the languages specified in the Content-Language header of this MimePart.
pack-priv static String[]
public String
getContentMD5()

Implements javax.mail.internet.MimePart.getContentMD5.

Return the value of the "Content-MD5" header field.
protected InputStream

Returns:

an InputStream containing the raw bytes
getContentStream
()

Produce the raw bytes of the content.

public String

Returns:

Content-Type of this body part
getContentType
()

Implements javax.mail.Part.getContentType.

Returns the value of the RFC 822 "Content-Type" header field.
public DataHandler
getDataHandler()

Implements javax.mail.Part.getDataHandler.

Return a DataHandler for this body part's content.
public String

Returns:

content description
getDescription
()

Implements javax.mail.Part.getDescription.

Returns the "Content-Description" header field of this body part.
pack-priv static String
public String
getDisposition()

Implements javax.mail.Part.getDisposition.

Returns the disposition from the "Content-Disposition" header field.
pack-priv static String
public String
getEncoding()

Implements javax.mail.internet.MimePart.getEncoding.

Returns the content transfer encoding from the "Content-Transfer-Encoding" header field.
pack-priv static String
public String

Returns:

filename
getFileName
()

Implements javax.mail.Part.getFileName.

Get the filename associated with this body part.
pack-priv static String
public String[]

Returns:

array of headers
getHeader
(String
name of header
name
)

Implements javax.mail.Part.getHeader.

Get all the headers for this header_name.
public String

Returns:

the value fields for all headers with this name
getHeader
(String
the name of this header
name
,
String
delimiter between fields in returned string
delimiter
)

Implements javax.mail.internet.MimePart.getHeader.

Get all the headers for this header name, returned as a single String, with headers separated by the delimiter.
public InputStream

Returns:

an InputStream
getInputStream
()

Implements javax.mail.Part.getInputStream.

Return a decoded input stream for this body part's "content".
public int

Returns:

number of lines, or -1 if not known
getLineCount
()

Implements javax.mail.Part.getLineCount.

Return the number of lines for the content of this Part.
public Enumeration<String>
getMatchingHeaderLines(String[]
the headers to return
names
)

Implements javax.mail.internet.MimePart.getMatchingHeaderLines.

Get matching header lines as an Enumeration of Strings.
public Enumeration<Header>
getMatchingHeaders(String[]
the headers to match
names
)

Implements javax.mail.Part.getMatchingHeaders.

Return matching headers from this Message as an Enumeration of Header objects.
public Enumeration<String>
getNonMatchingHeaderLines(String[]
the headers to not return
names
)

Implements javax.mail.internet.MimePart.getNonMatchingHeaderLines.

Get non-matching header lines as an Enumeration of Strings.
public Enumeration<Header>
getNonMatchingHeaders(String[]
the headers to not match
names
)

Implements javax.mail.Part.getNonMatchingHeaders.

Return non-matching headers from this Message as an Enumeration of Header objects.
public InputStream

Returns:

an InputStream containing the raw bytes
getRawInputStream
()

Return an InputStream to the raw data with any Content-Transfer-Encoding intact.

public int

Returns:

size in bytes, or -1 if not known
getSize
()

Implements javax.mail.Part.getSize.

Return the size of the content of this body part in bytes.
pack-priv static void
public boolean
isMimeType(String
the MIME type to test
mimeType
)

Implements javax.mail.Part.isMimeType.

Is this Part of the specified MIME type?
pack-priv static boolean
isMimeType(MimePart part, String mimeType)

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

Implements javax.mail.Part.removeHeader.

Remove all headers with this name.
pack-priv static String
restrictEncoding(MimePart part, String encoding)

Restrict the encoding to values allowed for the Content-Type of the specified MimePart.

public void
saveFile(File
the File object to write to
file
)

Save the contents of this part in the specified file.

public void
saveFile(String
the name of the file to write to
file
)

Save the contents of this part in the specified file.

public void
setContent(Object
the content object
o
,
String
Mime type of the object
type
)

Implements javax.mail.Part.setContent.

A convenience method for setting this body part's content.
public void
setContent(Multipart
The multipart object that is the Message's content
mp
)

Implements javax.mail.Part.setContent.

This method sets the body part's content to a Multipart object.
public void
setContentID(String
the Content-ID
cid
)

Set the "Content-ID" header field of this body part.

public void
setContentLanguage(String[]
array of language tags
languages
)

Implements javax.mail.internet.MimePart.setContentLanguage.

Set the Content-Language header of this MimePart.
pack-priv static void
setContentLanguage(MimePart part, String[] languages)

public void
setContentMD5(String
the MD5 value
md5
)

Implements javax.mail.internet.MimePart.setContentMD5.

Set the "Content-MD5" header field of this body part.
public void
setDataHandler(DataHandler
The DataHandler for the content
dh
)

Implements javax.mail.Part.setDataHandler.

This method provides the mechanism to set this body part's content.
public void
setDescription(String
content description
description
)

Implements javax.mail.Part.setDescription.

Set the "Content-Description" header field for this body part.
public void
setDescription(String
Description
description
,
String
Charset for encoding
charset
)

Set the "Content-Description" header field for this body part.

pack-priv static void
setDescription(MimePart part, String description, String charset)

public void
setDisposition(String
disposition of this part
disposition
)

Implements javax.mail.Part.setDisposition.

Set the disposition in the "Content-Disposition" header field of this body part.
pack-priv static void
setDisposition(MimePart part, String disposition)

pack-priv static void
setEncoding(MimePart part, String encoding)

public void
setFileName(String
the file name
filename
)

Implements javax.mail.Part.setFileName.

Set the filename associated with this body part, if possible.
pack-priv static void
public void
setHeader(String
header name
name
,
String
header value
value
)

Implements javax.mail.Part.setHeader.

Set the value for this header_name.
public void
setText(String
the text content to set
text
)

Implements javax.mail.internet.MimePart.setText, javax.mail.Part.setText.

Convenience method that sets the given String as this part's content, with a MIME type of "text/plain".
public void
setText(String
the text content to set
text
,
String
the charset to use for the text
charset
)

Implements javax.mail.internet.MimePart.setText.

Convenience method that sets the given String as this part's content, with a MIME type of "text/plain" and the specified charset.
public void
setText(String
the text content to set
text
,
String
the charset to use for the text
charset
,
String
the MIME subtype to use (e.g., "html")
subtype
)

Implements javax.mail.internet.MimePart.setText.

Convenience method that sets the given String as this part's content, with a primary MIME type of "text" and the specified MIME subtype.
pack-priv static void
setText(MimePart part, String text, String charset, String subtype)

protected void
updateHeaders()

Examine the content of this body part and update the appropriate MIME headers.

pack-priv static void
public void
writeTo(OutputStream
the stream to write to
os
)

Implements javax.mail.Part.writeTo.

Output the body part as an RFC 822 format stream.
pack-priv static void
writeTo(MimePart part, OutputStream os, String[] ignoreList)

Inherited from javax.mail.BodyPart:
getParent