javax.mail.internet
Class MimeBodyPart

java.lang.Object
  extended by javax.mail.BodyPart
      extended by javax.mail.internet.MimeBodyPart
All Implemented Interfaces:
MimePart, Part
Direct Known Subclasses:
IMAPBodyPart

public class MimeBodyPart
extends BodyPart
implements MimePart

A MIME body part. Body parts are components of multipart parts.

The string representation of RFC822 and MIME header fields must contain only US-ASCII characters. Non US-ASCII characters must be encoded as per the rules in RFC 2047. This class does not enforce those rules; the caller is expected to use MimeUtility to ensure that header values are correctly encoded.


Field Summary
protected  byte[] content
          The bytes of the content of this part, if the part can be stored in memory.
protected  java.io.InputStream contentStream
          A SharedInputStream containing the bytes of this part, if it cannot be stored in memory.
protected  DataHandler dh
          The data handler managing this part's content.
protected  InternetHeaders headers
          The headers of this body part.
 
Fields inherited from class javax.mail.BodyPart
parent
 
Fields inherited from interface javax.mail.Part
ATTACHMENT, INLINE
 
Constructor Summary
MimeBodyPart()
          Constructor for an empty MIME body part.
MimeBodyPart(java.io.InputStream is)
          Constructor with an input stream.
MimeBodyPart(InternetHeaders headers, byte[] content)
          Constructor with headers and byte content.
 
Method Summary
 void addHeader(java.lang.String name, java.lang.String value)
          Adds the specified header.
 void addHeaderLine(java.lang.String line)
          Adds an RFC 822 header-line to this part.
 java.util.Enumeration getAllHeaderLines()
          Returns all the header-lines.
 java.util.Enumeration getAllHeaders()
          Returns all the headers.
 java.lang.Object getContent()
          Returns this part's content as a Java object.
 java.lang.String getContentID()
          Returns the value of the Content-ID header field.
 java.lang.String[] getContentLanguage()
          Returns the languages specified in the Content-Language header of this part, as defined by RFC 1766.
 java.lang.String getContentMD5()
          Returns the value of the Content-MD5 header field.
protected  java.io.InputStream getContentStream()
          Returns the unencoded bytes of the content.
 java.lang.String getContentType()
          Returns the value of the RFC 822 Content-Type header field, or "text/plain" if the header is not available.
 DataHandler getDataHandler()
          Returns a data handler for accessing this part's content.
 java.lang.String getDescription()
          Returns the Content-Description header field of this part.
 java.lang.String getDisposition()
          Returns the value of the RFC 822 Content-Disposition header field, or null if the header is not available.
 java.lang.String getEncoding()
          Returns the value of the Content-Transfer-Encoding header field.
 java.lang.String getFileName()
          Returns the filename associated with this body part.
 java.lang.String[] getHeader(java.lang.String name)
          Returns all the values for the specified header name.
 java.lang.String getHeader(java.lang.String name, java.lang.String delimiter)
          Returns all the values for the specified header name as a single string, with headers separated by the given delimiter.
 java.io.InputStream getInputStream()
          Returns a decoded input stream for this part's content.
 int getLineCount()
          Returns the number of lines in the content of this body part, or -1 if this number cannot be determined.
 java.util.Enumeration getMatchingHeaderLines(java.lang.String[] names)
          Returns all the header-lines with any of the given names.
 java.util.Enumeration getMatchingHeaders(java.lang.String[] names)
          Returns all the headers with any of the given names.
 java.util.Enumeration getNonMatchingHeaderLines(java.lang.String[] names)
          Returns all the header-lines without any of the given names.
 java.util.Enumeration getNonMatchingHeaders(java.lang.String[] names)
          Returns all the headers without any of the given names.
 java.io.InputStream getRawInputStream()
          Returns the unencoded bytes of the content without applying any content transfer decoding.
 int getSize()
          Returns the size of the content of this body part in bytes, or -1 if the size cannot be determined.
 boolean isMimeType(java.lang.String mimeType)
          Indicates whether this part is of the specified MIME type.
 void removeHeader(java.lang.String name)
          Removes all headers with the specified name.
 void setContent(Multipart mp)
          Sets the content of this part to be the specified multipart.
 void setContent(java.lang.Object o, java.lang.String type)
          Sets the content of this part using the specified Java object and MIME type.
 void setContentID(java.lang.String cid)
          Sets the Content-ID header field of this part.
 void setContentLanguage(java.lang.String[] languages)
          Sets the Content-Language header of this part.
 void setContentMD5(java.lang.String md5)
          Sets the Content-MD5 header field of this part.
 void setDataHandler(DataHandler dh)
          Sets the content of this part using the specified data handler.
 void setDescription(java.lang.String description)
          Sets the Content-Description header field for this part.
 void setDescription(java.lang.String description, java.lang.String charset)
          Sets the Content-Description header field for this part.
 void setDisposition(java.lang.String disposition)
          Sets the Content-Disposition header field of this part.
 void setFileName(java.lang.String filename)
          Sets the filename associated with this body part.
 void setHeader(java.lang.String name, java.lang.String value)
          Sets the specified header.
 void setText(java.lang.String text)
          Sets the content of this part using the specified text, and with a MIME type of "text/plain".
 void setText(java.lang.String text, java.lang.String charset)
          Sets the content of this part using the specified text, and with a MIME type of "text/plain".
protected  void updateHeaders()
          Updates the headers of this part, based on the content.
 void writeTo(java.io.OutputStream os)
          Writes this body part to the specified stream in RFC 822 format.
 
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
 

Field Detail

dh

protected DataHandler dh
The data handler managing this part's content.


content

protected byte[] content
The bytes of the content of this part, if the part can be stored in memory.


contentStream

protected java.io.InputStream contentStream
A SharedInputStream containing the bytes of this part, if it cannot be stored in memory.


headers

protected InternetHeaders headers
The headers of this body part.

Constructor Detail

MimeBodyPart

public MimeBodyPart()
Constructor for an empty MIME body part.


MimeBodyPart

public MimeBodyPart(java.io.InputStream is)
             throws MessagingException
Constructor with an input stream. The stream must be positioned at the start of a valid MIME body part and terminate at the end of that body part: the boundary string must not be included in the stream.

Parameters:
is - the input stream
Throws:
MessagingException

MimeBodyPart

public MimeBodyPart(InternetHeaders headers,
                    byte[] content)
             throws MessagingException
Constructor with headers and byte content.

Parameters:
headers - the header
content - the byte content of this part
Throws:
MessagingException
Method Detail

getSize

public int getSize()
            throws MessagingException
Returns the size of the content of this body part in bytes, or -1 if the size cannot be determined.

Note that this number may not be an exact measure, but if not -1, it will be suitable for display to the user.

Specified by:
getSize in interface Part
Throws:
MessagingException

getLineCount

public int getLineCount()
                 throws MessagingException
Returns the number of lines in the content of this body part, or -1 if this number cannot be determined.

Note that this number may not be an exact measure, but if not -1, it will be suitable for display to the user.

Specified by:
getLineCount in interface Part
Throws:
MessagingException

getContentType

public java.lang.String getContentType()
                                throws MessagingException
Returns the value of the RFC 822 Content-Type header field, or "text/plain" if the header is not available.

Specified by:
getContentType in interface Part
Throws:
MessagingException

isMimeType

public boolean isMimeType(java.lang.String mimeType)
                   throws MessagingException
Indicates whether this part is of the specified MIME type.

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

Specified by:
isMimeType in interface Part
Throws:
MessagingException

getDisposition

public java.lang.String getDisposition()
                                throws MessagingException
Returns the value of the RFC 822 Content-Disposition header field, or null if the header is not available.

Specified by:
getDisposition in interface Part
Throws:
MessagingException

setDisposition

public void setDisposition(java.lang.String disposition)
                    throws MessagingException
Sets the Content-Disposition header field of this part.

Specified by:
setDisposition in interface Part
Parameters:
disposition - the disposition of this part
Throws:
IllegalWriteException - if the underlying implementation does not support modification
java.lang.IllegalStateException - if this body part is obtained from a READ_ONLY folder
MessagingException

getEncoding

public java.lang.String getEncoding()
                             throws MessagingException
Returns the value of the Content-Transfer-Encoding header field.

Specified by:
getEncoding in interface MimePart
Throws:
MessagingException

getContentID

public java.lang.String getContentID()
                              throws MessagingException
Returns the value of the Content-ID header field.

Specified by:
getContentID in interface MimePart
Throws:
MessagingException

setContentID

public void setContentID(java.lang.String cid)
                  throws MessagingException
Sets the Content-ID header field of this part.

Throws:
IllegalWriteException - if the underlying implementation does not support modification
java.lang.IllegalStateException - if this body part is obtained from a READ_ONLY folder
MessagingException
Since:
JavaMail 1.3

getContentMD5

public java.lang.String getContentMD5()
                               throws MessagingException
Returns the value of the Content-MD5 header field.

Specified by:
getContentMD5 in interface MimePart
Throws:
MessagingException

setContentMD5

public void setContentMD5(java.lang.String md5)
                   throws MessagingException
Sets the Content-MD5 header field of this part.

Specified by:
setContentMD5 in interface MimePart
Throws:
IllegalWriteException - if the underlying implementation does not support modification
java.lang.IllegalStateException - if this body part is obtained from a READ_ONLY folder
MessagingException

getContentLanguage

public java.lang.String[] getContentLanguage()
                                      throws MessagingException
Returns the languages specified in the Content-Language header of this part, as defined by RFC 1766. This method returns null if this header is not available.

Specified by:
getContentLanguage in interface MimePart
Throws:
MessagingException

setContentLanguage

public void setContentLanguage(java.lang.String[] languages)
                        throws MessagingException
Sets the Content-Language header of this part.

Specified by:
setContentLanguage in interface MimePart
Parameters:
languages - the array of language tags
Throws:
IllegalWriteException - if the underlying implementation does not support modification
java.lang.IllegalStateException - if this body part is obtained from a READ_ONLY folder
MessagingException

getDescription

public java.lang.String getDescription()
                                throws MessagingException
Returns the Content-Description header field of this part.

If the Content-Description field is encoded as per RFC 2047, it is decoded and converted into Unicode.

Specified by:
getDescription in interface Part
Throws:
MessagingException

setDescription

public void setDescription(java.lang.String description)
                    throws MessagingException
Sets the Content-Description header field for this part.

If description contains non US-ASCII characters, it will be encoded using the platform default charset.

Specified by:
setDescription in interface Part
Parameters:
description - the content description
Throws:
IllegalWriteException - if the underlying implementation does not support modification
java.lang.IllegalStateException - if this body part is obtained from a READ_ONLY folder
MessagingException

setDescription

public void setDescription(java.lang.String description,
                           java.lang.String charset)
                    throws MessagingException
Sets the Content-Description header field for this part.

If description contains non US-ASCII characters, it will be encoded using the specified charset.

Parameters:
description - the content description
charset - the charset used for encoding
Throws:
IllegalWriteException - if the underlying implementation does not support modification
java.lang.IllegalStateException - if this body part is obtained from a READ_ONLY folder
MessagingException

getFileName

public java.lang.String getFileName()
                             throws MessagingException
Returns the filename associated with this body part.

This method returns the value of the "filename" parameter from the Content-Disposition header field. If the latter is not available, it returns the value of the "name" parameter from the Content-Type header field.

Specified by:
getFileName in interface Part
Throws:
MessagingException

setFileName

public void setFileName(java.lang.String filename)
                 throws MessagingException
Sets the filename associated with this body part.

Specified by:
setFileName in interface Part
Parameters:
filename - the filename to associate with this part
Throws:
IllegalWriteException - if the underlying implementation does not support modification
java.lang.IllegalStateException - if this body part is obtained from a READ_ONLY folder
MessagingException

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.IOException,
                                          MessagingException
Returns a decoded input stream for this part's content.

Specified by:
getInputStream in interface Part
Throws:
java.io.IOException - if an error occurs in the data handler layer
MessagingException

getContentStream

protected java.io.InputStream getContentStream()
                                        throws MessagingException
Returns the unencoded bytes of the content.

Throws:
MessagingException

getRawInputStream

public java.io.InputStream getRawInputStream()
                                      throws MessagingException
Returns the unencoded bytes of the content without applying any content transfer decoding.

Throws:
MessagingException

getDataHandler

public DataHandler getDataHandler()
                           throws MessagingException
Returns a data handler for accessing this part's content.

Specified by:
getDataHandler in interface Part
Throws:
MessagingException

getContent

public java.lang.Object getContent()
                            throws java.io.IOException,
                                   MessagingException
Returns this part's content as a Java object.

Specified by:
getContent in interface Part
Throws:
java.io.IOException - if an error occurred in the data handler layer
MessagingException

setDataHandler

public void setDataHandler(DataHandler dh)
                    throws MessagingException
Sets the content of this part using the specified data handler.

Specified by:
setDataHandler in interface Part
Parameters:
dh - the data handler for the content
Throws:
IllegalWriteException - if the underlying implementation does not support modification
java.lang.IllegalStateException - if this body part is obtained from a READ_ONLY folder
MessagingException

setContent

public void setContent(java.lang.Object o,
                       java.lang.String type)
                throws MessagingException
Sets the content of this part using the specified Java object and MIME type. Note that a data content handler for the MIME type must be installed and accept objects of the type given.

Specified by:
setContent in interface Part
Parameters:
o - the content object
type - the MIME type of the object
Throws:
IllegalWriteException - if the underlying implementation does not support modification
java.lang.IllegalStateException - if this body part is obtained from a READ_ONLY folder
MessagingException

setText

public void setText(java.lang.String text)
             throws MessagingException
Sets the content of this part using the specified text, and with a MIME type of "text/plain".

If the text contains non US-ASCII characters, it will be encoded using the platform default charset.

Specified by:
setText in interface MimePart
Specified by:
setText in interface Part
Parameters:
text - the text content
Throws:
IllegalWriteException - if the underlying implementation does not support modification
MessagingException

setText

public void setText(java.lang.String text,
                    java.lang.String charset)
             throws MessagingException
Sets the content of this part using the specified text, and with a MIME type of "text/plain".

If the text contains non US-ASCII characters, it will be encoded using the specified charset.

Specified by:
setText in interface MimePart
Parameters:
text - the text content
charset - the charset used for any encoding
Throws:
MessagingException

setContent

public void setContent(Multipart mp)
                throws MessagingException
Sets the content of this part to be the specified multipart.

Specified by:
setContent in interface Part
Parameters:
mp - the multipart content
Throws:
IllegalWriteException - if the underlying implementation does not support modification
java.lang.IllegalStateException - if this body part is obtained from a READ_ONLY folder
MessagingException

writeTo

public void writeTo(java.io.OutputStream os)
             throws java.io.IOException,
                    MessagingException
Writes this body part to the specified stream in RFC 822 format.

Specified by:
writeTo in interface Part
Throws:
java.io.IOException - if an error occurs writing to the stream or in the data handler layer
MessagingException - if an error occurs fetching the data to be written

getHeader

public java.lang.String[] getHeader(java.lang.String name)
                             throws MessagingException
Returns all the values for the specified header name. Note that headers may be encoded as per RFC 2047 if they contain non-US-ASCII characters: these should be decoded.

Specified by:
getHeader in interface Part
Parameters:
name - the header name
Throws:
MessagingException

getHeader

public java.lang.String getHeader(java.lang.String name,
                                  java.lang.String delimiter)
                           throws MessagingException
Returns all the values for the specified header name as a single string, with headers separated by the given delimiter. If the delimiter is null, only the first header is returned.

Specified by:
getHeader in interface MimePart
Parameters:
name - the header name
delimiter - the delimiter
Throws:
MessagingException

setHeader

public void setHeader(java.lang.String name,
                      java.lang.String value)
               throws MessagingException
Sets the specified header.

Specified by:
setHeader in interface Part
Parameters:
name - the header name
value - the header value
Throws:
IllegalWriteException - if the underlying implementation does not support modification
java.lang.IllegalStateException - if this body part is obtained from a READ_ONLY folder
MessagingException

addHeader

public void addHeader(java.lang.String name,
                      java.lang.String value)
               throws MessagingException
Adds the specified header.

Specified by:
addHeader in interface Part
Parameters:
name - the header name
value - the header value
Throws:
IllegalWriteException - if the underlying implementation does not support modification
java.lang.IllegalStateException - if this body part is obtained from a READ_ONLY folder
MessagingException

removeHeader

public void removeHeader(java.lang.String name)
                  throws MessagingException
Removes all headers with the specified name.

Specified by:
removeHeader in interface Part
Parameters:
name - the header name
Throws:
IllegalWriteException - if the underlying implementation does not support modification
java.lang.IllegalStateException - if this body part is obtained from a READ_ONLY folder
MessagingException

getAllHeaders

public java.util.Enumeration getAllHeaders()
                                    throws MessagingException
Returns all the headers.

Specified by:
getAllHeaders in interface Part
Returns:
an Enumeration of Header objects
Throws:
MessagingException

getMatchingHeaders

public java.util.Enumeration getMatchingHeaders(java.lang.String[] names)
                                         throws MessagingException
Returns all the headers with any of the given names.

Specified by:
getMatchingHeaders in interface Part
Parameters:
names - the header names to match
Returns:
an Enumeration of Header objects
Throws:
MessagingException

getNonMatchingHeaders

public java.util.Enumeration getNonMatchingHeaders(java.lang.String[] names)
                                            throws MessagingException
Returns all the headers without any of the given names.

Specified by:
getNonMatchingHeaders in interface Part
Parameters:
names - the header names to ignore
Returns:
an Enumeration of Header objects
Throws:
MessagingException

addHeaderLine

public void addHeaderLine(java.lang.String line)
                   throws MessagingException
Adds an RFC 822 header-line to this part.

Specified by:
addHeaderLine in interface MimePart
Throws:
IllegalWriteException - if the underlying implementation does not support modification
java.lang.IllegalStateException - if this body part is obtained from a READ_ONLY folder
MessagingException

getAllHeaderLines

public java.util.Enumeration getAllHeaderLines()
                                        throws MessagingException
Returns all the header-lines.

Specified by:
getAllHeaderLines in interface MimePart
Returns:
an Enumeration of Strings
Throws:
MessagingException

getMatchingHeaderLines

public java.util.Enumeration getMatchingHeaderLines(java.lang.String[] names)
                                             throws MessagingException
Returns all the header-lines with any of the given names.

Specified by:
getMatchingHeaderLines in interface MimePart
Returns:
an Enumeration of Strings
Throws:
MessagingException

getNonMatchingHeaderLines

public java.util.Enumeration getNonMatchingHeaderLines(java.lang.String[] names)
                                                throws MessagingException
Returns all the header-lines without any of the given names.

Specified by:
getNonMatchingHeaderLines in interface MimePart
Returns:
an Enumeration of Strings
Throws:
MessagingException

updateHeaders

protected void updateHeaders()
                      throws MessagingException
Updates the headers of this part, based on the content.

Throws:
IllegalWriteException - if the underlying implementation does not support modification
java.lang.IllegalStateException - if this body part is obtained from a READ_ONLY folder
MessagingException