java.lang.Object | +--javax.mail.BodyPart | +--javax.mail.internet.MimeBodyPart
This class represents a MIME body part. It implements theBodyPart
abstract class and theMimePart
interface. MimeBodyParts are contained inMimeMultipart
objects.
MimeBodyPart uses theInternetHeaders
class to parse and store the headers of that body part.
RFC 822 header fieldsmust 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.
Part
,MimePart
,MimeUtility
content Byte array that holds the bytes of the content of this Part. | |
dh The DataHandler object representing this Part's content. | |
headers The InternetHeaders object that stores all the headers of this body part. |
Fields inherited from class javax.mail.BodyPart |
parent |
Fields inherited from interface javax.mail.Part |
ATTACHMENT,INLINE |
MimeBodyPart() An empty MimeBodyPart object is created. | |
MimeBodyPart(java.io.InputStream is) Constructs a MimeBodyPart by reading and parsing the data from the specified input stream. | |
MimeBodyPart(InternetHeaders headers, byte[] content) Constructs a MimeBodyPart using the given header and content bytes. |
addHeader(java.lang.String name, java.lang.String value) Add this value to the existing values for this header_name. | |
addHeaderLine(java.lang.String line) Add a header line to this body part | |
getAllHeaderLines() Get all header lines as an Enumeration of Strings. | |
getAllHeaders() Return all the headers from this Message as an Enumeration of Header objects. | |
getContent() Return the content as a java object. | |
getContentID() Returns the value of the "Content-ID" header field. | |
getContentLanguage() Get the languages specified in the Content-Language header of this MimePart. | |
getContentMD5() Return the value of the "Content-MD5" header field. | |
getContentStream() Produce the raw bytes of the content. | |
getContentType() Returns the value of the RFC 822 "Content-Type" header field. | |
getDataHandler() Return a DataHandler for this body part's content. | |
getDescription() Returns the "Content-Description" header field of this body part. | |
getDisposition() Returns the value of the "Content-Disposition" header field. | |
getEncoding() Returns the content transfer encoding from the "Content-Transfer-Encoding" header field. | |
getFileName() Get the filename associated with this body part. | |
getHeader(java.lang.String name) Get all the headers for this header_name. | |
getHeader(java.lang.String name, java.lang.String delimiter) Get all the headers for this header name, returned as a single String, with headers separated by the delimiter. | |
getInputStream() Return a decoded input stream for this body part's "content". | |
getLineCount() Return the number of lines for the content of this Part. | |
getMatchingHeaderLines(java.lang.String[] names) Get matching header lines as an Enumeration of Strings. | |
getMatchingHeaders(java.lang.String[] names) Return matching headers from this Message as an Enumeration of Header objects. | |
getNonMatchingHeaderLines(java.lang.String[] names) Get non-matching header lines as an Enumeration of Strings. | |
getNonMatchingHeaders(java.lang.String[] names) Return non-matching headers from this Message as an Enumeration of Header objects. | |
getSize() Return the size of the content of this body part in bytes. | |
isMimeType(java.lang.String mimeType) Is this Part of the specified MIME type? This method comparesonly the primaryType andsubType . | |
removeHeader(java.lang.String name) Remove all headers with this name. | |
setContent(Multipart mp) This method sets the body part's content to a Multipart object. | |
setContent(java.lang.Object o, java.lang.String type) A convenience method for setting this body part's content. | |
setContentLanguage(java.lang.String[] languages) Set the Content-Language header of this MimePart. | |
setContentMD5(java.lang.String md5) Set the "Content-MD5" header field of this body part. | |
setDataHandler(DataHandler dh) This method provides the mechanism to set this body part's content. | |
setDescription(java.lang.String description) Set the "Content-Description" header field for this body part. | |
setDescription(java.lang.String description, java.lang.String charset) Set the "Content-Description" header field for this body part. | |
setDisposition(java.lang.String disposition) Set the "Content-Disposition" header field of this body part. | |
setFileName(java.lang.String filename) Set the filename associated with this body part, if possible. | |
setHeader(java.lang.String name, java.lang.String value) Set the value for this header_name. | |
setText(java.lang.String text) Convenience method that sets the given String as this part's content, with a MIME type of "text/plain". | |
setText(java.lang.String text, java.lang.String charset) Convenience method that sets the given String as this part's content, with a MIME type of "text/plain" and the specified charset. | |
updateHeaders() Examine the content of this body part and update the appropriate MIME headers. | |
writeTo(java.io.OutputStream os) Output the body part as an RFC 822 format stream. |
Methods inherited from class javax.mail.BodyPart |
getParent |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
protectedDataHandlerdh
protected byte[]content
protectedInternetHeadersheaders
publicMimeBodyPart()
publicMimeBodyPart(java.io.InputStream is) throwsMessagingException
Note that the "boundary" string that delimits body parts mustnot be included in the input stream. The intention is that the MimeMultipart parser will extract each body part's bytes from a multipart stream and feed them into this constructor, without the delimiter strings.
is
- the body part Input StreampublicMimeBodyPart(InternetHeaders headers, byte[] content) throwsMessagingException
Used by providers.
headers
- The header of this partcontent
- bytes representing the body of this part.public intgetSize() throwsMessagingException
Note that this number may not be an exact measure of the content size and may or may not account for any transfer encoding of the content.
This implementation returns -1;
getSize
in interfacePart
public intgetLineCount() throwsMessagingException
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.
This implementation returns -1;
getLineCount
in interfacePart
javax.mail.Part
MessagingException
- public java.lang.StringgetContentType() throwsMessagingException
This implementation usesgetHeader(name)
to obtain the requisite header field.
getContentType
in interfacePart
public booleanisMimeType(java.lang.String mimeType) throwsMessagingException
primaryType
andsubType
. The parameters of the content types are ignored. For example, this method will returntrue
when comparing a Part of content type"text/plain" with"text/plain; charset=foobar".
If thesubType
ofmimeType
is the special character '*', then the subtype is ignored during the comparison.
isMimeType
in interfacePart
public java.lang.StringgetDisposition() throwsMessagingException
If the Content-Disposition field is unavailable, null is returned.
This implementation usesgetHeader(name)
to obtain the requisite header field.
getDisposition
in interfacePart
headers
public voidsetDisposition(java.lang.String disposition) throwsMessagingException
setDisposition
in interfacePart
IllegalWriteException
- if the underlyingimplementation does not support modificationjava.lang.IllegalStateException
- if this body part isobtained from a READ_ONLY folder.public java.lang.StringgetEncoding() throwsMessagingException
null
if the header is unavailable or its value is absent. This implementation usesgetHeader(name)
to obtain the requisite header field.
getEncoding
in interfaceMimePart
headers
public java.lang.StringgetContentID() throwsMessagingException
null
if the field is unavailable or its value is absent. This implementation usesgetHeader(name)
to obtain the requisite header field.
getContentID
in interfaceMimePart
javax.mail.internet.MimePart
public java.lang.StringgetContentMD5() throwsMessagingException
null
if this field is unavailable or its value is absent. This implementation usesgetHeader(name)
to obtain the requisite header field.
getContentMD5
in interfaceMimePart
javax.mail.internet.MimePart
public voidsetContentMD5(java.lang.String md5) throwsMessagingException
setContentMD5
in interfaceMimePart
IllegalWriteException
- if the underlyingimplementation does not support modificationjava.lang.IllegalStateException
- if this body part isobtained from a READ_ONLY folder.public java.lang.String[]getContentLanguage() throwsMessagingException
null
if this header is not available or its value is absent. This implementation usesgetHeader(name)
to obtain the requisite header field.
getContentLanguage
in interfaceMimePart
public voidsetContentLanguage(java.lang.String[] languages) throwsMessagingException
setContentLanguage
in interfaceMimePart
languages
- array of language tagspublic java.lang.StringgetDescription() throwsMessagingException
If the Content-Description field is encoded as per RFC 2047, it is decoded and converted into Unicode. If the decoding or conversion fails, the raw data is returned as is.
This implementation usesgetHeader(name)
to obtain the requisite header field.
getDescription
in interfacePart
public voidsetDescription(java.lang.String description) throwsMessagingException
null
, then any existing "Content-Description" fields are removed.If the description contains non US-ASCII characters, it will be encoded using the platform's default charset. If the description contains only US-ASCII characters, no encoding is done and it is used as is.
Note that if the charset encoding process fails, a MessagingException is thrown, and an UnsupportedEncodingException is included in the chain of nested exceptions within the MessagingException.
setDescription
in interfacePart
description
- content descriptionIllegalWriteException
- if the underlyingimplementation does not support modificationjava.lang.IllegalStateException
- if this body part isobtained from a READ_ONLY folder.MessagingException.
- An UnsupportedEncodingException may be included in the exception chain if the charset conversion fails.public voidsetDescription(java.lang.String description, java.lang.String charset) throwsMessagingException
null
, then any existing "Content-Description" fields are removed.If the description contains non US-ASCII characters, it will be encoded using the specified charset. If the description contains only US-ASCII characters, no encoding is done and it is used as is.
Note that if the charset encoding process fails, a MessagingException is thrown, and an UnsupportedEncodingException is included in the chain of nested exceptions within the MessagingException.
description
- Descriptioncharset
- Charset for encodingIllegalWriteException
- if the underlyingimplementation does not support modificationjava.lang.IllegalStateException
- if this body part isobtained from a READ_ONLY folder.MessagingException.
- An UnsupportedEncodingException may be included in the exception chain if the charset conversion fails.public java.lang.StringgetFileName() throwsMessagingException
Returns the value of the "filename" parameter from the "Content-Disposition" header field of this body part. If its not available, returns the value of the "name" parameter from the "Content-Type" header field of this body part. Returnsnull
if both are absent.
getFileName
in interfacePart
public voidsetFileName(java.lang.String filename) throwsMessagingException
Sets the "filename" parameter of the "Content-Disposition" header field of this body part.
setFileName
in interfacePart
IllegalWriteException
- if the underlyingimplementation does not support modificationjava.lang.IllegalStateException
- if this body part isobtained from a READ_ONLY folder.public java.io.InputStreamgetInputStream() throws java.io.IOException,MessagingException
This implementation obtains the input stream from the DataHandler. That is, it invokes getDataHandler().getInputStream();
getInputStream
in interfacePart
MessagingException
- java.io.IOException
- this is typically thrown by theDataHandler. Refer to the documentation forjavax.activation.DataHandler for more details.getContentStream()
,DataHandler.getInputStream()
protected java.io.InputStreamgetContentStream() throwsMessagingException
content
,MimeMessage.getContentStream()
publicDataHandlergetDataHandler() throwsMessagingException
The implementation provided here works just like the the implementation in MimeMessage.
getDataHandler
in interfacePart
MimeMessage.getDataHandler()
public java.lang.ObjectgetContent() throws java.io.IOException,MessagingException
This implementation obtains the content from the DataHandler. That is, it invokes getDataHandler().getContent();
getContent
in interfacePart
MessagingException
- java.io.IOException
- this is typically thrown by theDataHandler. Refer to the documentation forjavax.activation.DataHandler for more details.public voidsetDataHandler(DataHandler dh) throwsMessagingException
setDataHandler
in interfacePart
dh
- The DataHandler for the contentIllegalWriteException
- if the underlying implementation does not support modificationjava.lang.IllegalStateException
- if this body part isobtained from a READ_ONLY folder.public voidsetContent(java.lang.Object o, java.lang.String type) throwsMessagingException
The content is wrapped in a DataHandler object. Note that a DataContentHandler class for the specified type should be available to the JavaMail implementation for this to work right. That is, to dosetContent(foobar, "application/x-foobar")
, a DataContentHandler for "application/x-foobar" should be installed. Refer to the Java Activation Framework for more information.
setContent
in interfacePart
o
- the content objecttype
- Mime type of the objectIllegalWriteException
- if the underlyingimplementation does not support modification ofexisting valuesjava.lang.IllegalStateException
- if this body part isobtained from a READ_ONLY folder.public voidsetText(java.lang.String text) throwsMessagingException
Note that there may be a performance penalty iftext
is large, since this method may have to scan all the characters to determine what charset to use.
If the charset is already known, use the setText() version that takes the charset parameter.
setText
in interfaceMimePart
setText(String text, String charset)
public voidsetText(java.lang.String text, java.lang.String charset) throwsMessagingException
setText
in interfaceMimePart
public voidsetContent(Multipart mp) throwsMessagingException
setContent
in interfacePart
mp
- The multipart object that is the Message's contentIllegalWriteException
- if the underlyingimplementation does not support modification ofexisting values.java.lang.IllegalStateException
- if this body part isobtained from a READ_ONLY folder.public voidwriteTo(java.io.OutputStream os) throws java.io.IOException,MessagingException
writeTo
in interfacePart
MessagingException
- java.io.IOException
- if an error occurs writing to thestream or if an error is generatedby the javax.activation layer.DataHandler.writeTo(java.io.OutputStream)
public java.lang.String[]getHeader(java.lang.String name) throwsMessagingException
getHeader
in interfacePart
name
- name of headerMimeUtility
public java.lang.StringgetHeader(java.lang.String name, java.lang.String delimiter) throwsMessagingException
null
, only the first header is returned.getHeader
in interfaceMimePart
header_name
- the name of this headerMessagingException
- public voidsetHeader(java.lang.String name, java.lang.String value) throwsMessagingException
setHeader
in interfacePart
name
- header namevalue
- header valueMimeUtility
public voidaddHeader(java.lang.String name, java.lang.String value) throwsMessagingException
addHeader
in interfacePart
name
- header namevalue
- header valueMimeUtility
public voidremoveHeader(java.lang.String name) throwsMessagingException
removeHeader
in interfacePart
javax.mail.Part
header_name
- the name of this headerMessagingException
- IllegalWriteException
- if the underlying implementation does not support modification of existing valuesjava.lang.IllegalStateException
- if this Part is obtained from a READ_ONLY folderpublic java.util.EnumerationgetAllHeaders() throwsMessagingException
getAllHeaders
in interfacePart
javax.mail.Part
MessagingException
- public java.util.EnumerationgetMatchingHeaders(java.lang.String[] names) throwsMessagingException
getMatchingHeaders
in interfacePart
javax.mail.Part
MessagingException
- public java.util.EnumerationgetNonMatchingHeaders(java.lang.String[] names) throwsMessagingException
getNonMatchingHeaders
in interfacePart
javax.mail.Part
MessagingException
- public voidaddHeaderLine(java.lang.String line) throwsMessagingException
addHeaderLine
in interfaceMimePart
javax.mail.internet.MimePart
IllegalWriteException
- if the underlyingimplementation does not support modificationjava.lang.IllegalStateException
- if this Part isobtained from a READ_ONLY folderpublic java.util.EnumerationgetAllHeaderLines() throwsMessagingException
getAllHeaderLines
in interfaceMimePart
public java.util.EnumerationgetMatchingHeaderLines(java.lang.String[] names) throwsMessagingException
getMatchingHeaderLines
in interfaceMimePart
public java.util.EnumerationgetNonMatchingHeaderLines(java.lang.String[] names) throwsMessagingException
getNonMatchingHeaderLines
in interfaceMimePart
protected voidupdateHeaders() throwsMessagingException
Content-Type
andContent-Transfer-Encoding
. Headers might need to be updated in two cases:Message.saveChanges
method.