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:
PreencodedMimeBodyPart

public class MimeBodyPart
extends BodyPart
implements MimePart

Version:
$Rev: 689126 $ $Date: 2008-08-26 12:17:53 -0400 (Tue, 26 Aug 2008) $

Field Summary
protected  byte[] content
          This message's content (unless sourced from a SharedInputStream).
protected  InputStream contentStream
          If the data for this message was supplied by a SharedInputStream then this is another such stream representing the content of this message; if this field is non-null, then content will be null.
protected  DataHandler dh
          The DataHandler for this Message's content.
protected  InternetHeaders headers
          This message's headers.
 
Fields inherited from class javax.mail.BodyPart
parent
 
Fields inherited from interface javax.mail.Part
ATTACHMENT, INLINE
 
Constructor Summary
MimeBodyPart()
           
MimeBodyPart(InputStream in)
           
MimeBodyPart(InternetHeaders headers, byte[] content)
           
 
Method Summary
 void addHeader(String name, String value)
          Add this value to the existing headers with the given name.
 void addHeaderLine(String line)
           
 void attachFile(File file)
          Attach a file to this body part from a File object.
 void attachFile(String file)
          Attach a file to this body part from a file name.
 Enumeration getAllHeaderLines()
           
 Enumeration getAllHeaders()
          Return all headers as an Enumeration of Header objects.
 Object getContent()
          Return a content object for this Part.
 String getContentID()
          Retrieve the value of the "Content-ID" header.
 String[] getContentLanguage()
           
 String getContentMD5()
           
protected  InputStream getContentStream()
           
 String getContentType()
          Get the ContentType for this part, or null if the ContentType has not been set.
 DataHandler getDataHandler()
          Returns a DataHandler instance for the content with in the Part.
 String getDescription()
          Returns a description string for this Part.
 String getDisposition()
          Retrieve the message "Content-Disposition" header field.
 String getEncoding()
          Retrieves the current value of the "Content-Transfer-Encoding" header.
 String getFileName()
          Get a file name associated with this part.
 String[] getHeader(String name)
          Get all Headers for this header name.
 String getHeader(String name, String delimiter)
           
 InputStream getInputStream()
          Return an InputStream for accessing the Part content.
 int getLineCount()
          Return the number of lines in the content, or -1 if the line count cannot be determined.
 Enumeration getMatchingHeaderLines(String[] names)
           
 Enumeration getMatchingHeaders(String[] name)
          Return all headers that match the list of names as an Enumeration of Header objects.
 Enumeration getNonMatchingHeaderLines(String[] names)
           
 Enumeration getNonMatchingHeaders(String[] name)
          Return an Enumeration of all Headers except those that match the names given in the exclusion list.
 InputStream getRawInputStream()
           
 int getSize()
          Return the content size of this message.
 boolean isMimeType(String type)
          Tests to see if this message has a mime-type match with the given type name.
 void removeHeader(String name)
          Remove all headers with the given name from the Part.
 void saveFile(File file)
          Save the body part content to a given target file.
 void saveFile(String file)
          Save the body part content to a given target file.
 void setContent(Multipart part)
           
 void setContent(Object content, String type)
          Set a content object for this part.
 void setContentID(String cid)
           
 void setContentLanguage(String[] languages)
           
 void setContentMD5(String md5)
           
 void setDataHandler(DataHandler handler)
          Set a DataHandler for this part that defines the Part content.
 void setDescription(String description)
          Set a descriptive string for this part.
 void setDescription(String description, String charset)
           
 void setDisposition(String disposition)
          Set a new dispostion value for the "Content-Disposition" field.
 void setFileName(String name)
          Set a descriptive file name for this part.
 void setHeader(String name, String value)
          Sets a value for the given header.
 void setText(String text)
          Set the Part content as text.
 void setText(String text, String charset)
           
 void setText(String text, String charset, String subtype)
           
protected  void updateHeaders()
           
 void writeTo(OutputStream out)
          Write the Part content out to the provided OutputStream as a byte stream using an encoding appropriate to the Part content.
 
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 DataHandler for this Message's content.


content

protected byte[] content
This message's content (unless sourced from a SharedInputStream).


contentStream

protected InputStream contentStream
If the data for this message was supplied by a SharedInputStream then this is another such stream representing the content of this message; if this field is non-null, then content will be null.


headers

protected InternetHeaders headers
This message's headers.

Constructor Detail

MimeBodyPart

public MimeBodyPart()

MimeBodyPart

public MimeBodyPart(InputStream in)
             throws MessagingException
Throws:
MessagingException

MimeBodyPart

public MimeBodyPart(InternetHeaders headers,
                    byte[] content)
             throws MessagingException
Throws:
MessagingException
Method Detail

getSize

public int getSize()
            throws MessagingException
Return the content size of this message. This is obtained either from the size of the content field (if available) or from the contentStream, IFF the contentStream returns a positive size. Returns -1 if the size is not available.

Specified by:
getSize in interface Part
Returns:
Size of the content in bytes.
Throws:
MessagingException

getLineCount

public int getLineCount()
                 throws MessagingException
Description copied from interface: Part
Return the number of lines in the content, or -1 if the line count cannot be determined.

Specified by:
getLineCount in interface Part
Returns:
The estimated number of lines in the content.
Throws:
MessagingException

getContentType

public String getContentType()
                      throws MessagingException
Description copied from interface: Part
Get the ContentType for this part, or null if the ContentType has not been set. The ContentType is expressed using the MIME typing system.

Specified by:
getContentType in interface Part
Returns:
The ContentType for this part.
Throws:
MessagingException

isMimeType

public boolean isMimeType(String type)
                   throws MessagingException
Tests to see if this message has a mime-type match with the given type name.

Specified by:
isMimeType in interface Part
Parameters:
type - The tested type name.
Returns:
If this is a type match on the primary and secondare portion of the types.
Throws:
MessagingException

getDisposition

public String getDisposition()
                      throws MessagingException
Retrieve the message "Content-Disposition" header field. This value represents how the part should be represented to the user.

Specified by:
getDisposition in interface Part
Returns:
The string value of the Content-Disposition field.
Throws:
MessagingException

setDisposition

public void setDisposition(String disposition)
                    throws MessagingException
Set a new dispostion value for the "Content-Disposition" field. If the new value is null, the header is removed.

Specified by:
setDisposition in interface Part
Parameters:
disposition - The new disposition value.
Throws:
MessagingException

getEncoding

public String getEncoding()
                   throws MessagingException
Retrieves the current value of the "Content-Transfer-Encoding" header. Returns null if the header does not exist.

Specified by:
getEncoding in interface MimePart
Returns:
The current header value or null.
Throws:
MessagingException

getContentID

public String getContentID()
                    throws MessagingException
Retrieve the value of the "Content-ID" header. Returns null if the header does not exist.

Specified by:
getContentID in interface MimePart
Returns:
The current header value or null.
Throws:
MessagingException

setContentID

public void setContentID(String cid)
                  throws MessagingException
Throws:
MessagingException

getContentMD5

public String getContentMD5()
                     throws MessagingException
Specified by:
getContentMD5 in interface MimePart
Throws:
MessagingException

setContentMD5

public void setContentMD5(String md5)
                   throws MessagingException
Specified by:
setContentMD5 in interface MimePart
Throws:
MessagingException

getContentLanguage

public String[] getContentLanguage()
                            throws MessagingException
Specified by:
getContentLanguage in interface MimePart
Throws:
MessagingException

setContentLanguage

public void setContentLanguage(String[] languages)
                        throws MessagingException
Specified by:
setContentLanguage in interface MimePart
Throws:
MessagingException

getDescription

public String getDescription()
                      throws MessagingException
Description copied from interface: Part
Returns a description string for this Part. Returns null if a description has not been set.

Specified by:
getDescription in interface Part
Returns:
The description string.
Throws:
MessagingException

setDescription

public void setDescription(String description)
                    throws MessagingException
Description copied from interface: Part
Set a descriptive string for this part.

Specified by:
setDescription in interface Part
Parameters:
description - The new description.
Throws:
MessagingException

setDescription

public void setDescription(String description,
                           String charset)
                    throws MessagingException
Throws:
MessagingException

getFileName

public String getFileName()
                   throws MessagingException
Description copied from interface: Part
Get a file name associated with this part. The file name is useful for presenting attachment parts as their original source. The file names are generally simple names without containing any directory information. Returns null if the filename has not been set.

Specified by:
getFileName in interface Part
Returns:
The string filename, if any.
Throws:
MessagingException

setFileName

public void setFileName(String name)
                 throws MessagingException
Description copied from interface: Part
Set a descriptive file name for this part. The name should be a simple name that does not include directory information.

Specified by:
setFileName in interface Part
Parameters:
name - The new name value.
Throws:
MessagingException

getInputStream

public InputStream getInputStream()
                           throws MessagingException,
                                  IOException
Description copied from interface: Part
Return an InputStream for accessing the Part content. Any mail-related transfer encodings will be removed, so the data presented with be the actual part content.

Specified by:
getInputStream in interface Part
Returns:
An InputStream for accessing the part content.
Throws:
MessagingException
IOException

getContentStream

protected InputStream getContentStream()
                                throws MessagingException
Throws:
MessagingException

getRawInputStream

public InputStream getRawInputStream()
                              throws MessagingException
Throws:
MessagingException

getDataHandler

public DataHandler getDataHandler()
                           throws MessagingException
Description copied from interface: Part
Returns a DataHandler instance for the content with in the Part.

Specified by:
getDataHandler in interface Part
Returns:
A DataHandler appropriate for the Part content.
Throws:
MessagingException

getContent

public Object getContent()
                  throws MessagingException,
                         IOException
Description copied from interface: Part
Return a content object for this Part. The content object type is dependent upon the DataHandler for the Part.

Specified by:
getContent in interface Part
Returns:
A content object for this Part.
Throws:
MessagingException
IOException

setDataHandler

public void setDataHandler(DataHandler handler)
                    throws MessagingException
Description copied from interface: Part
Set a DataHandler for this part that defines the Part content. The DataHandler is used to access all Part content.

Specified by:
setDataHandler in interface Part
Parameters:
handler - The DataHandler instance.
Throws:
MessagingException

setContent

public void setContent(Object content,
                       String type)
                throws MessagingException
Description copied from interface: Part
Set a content object for this part. Internally, the Part will use the MIME type encoded in the type argument to wrap the provided content object. In order for this to work properly, an appropriate DataHandler must be installed in the Java Activation Framework.

Specified by:
setContent in interface Part
Parameters:
content - The content object.
type - The MIME type for the inserted content Object.
Throws:
MessagingException

setText

public void setText(String text)
             throws MessagingException
Description copied from interface: Part
Set the Part content as text. This is a convenience method that sets the content to a MIME type of "text/plain".

Specified by:
setText in interface MimePart
Specified by:
setText in interface Part
Parameters:
text - The new text content, as a String object.
Throws:
MessagingException

setText

public void setText(String text,
                    String charset)
             throws MessagingException
Specified by:
setText in interface MimePart
Throws:
MessagingException

setText

public void setText(String text,
                    String charset,
                    String subtype)
             throws MessagingException
Specified by:
setText in interface MimePart
Throws:
MessagingException

setContent

public void setContent(Multipart part)
                throws MessagingException
Specified by:
setContent in interface Part
Throws:
MessagingException

writeTo

public void writeTo(OutputStream out)
             throws IOException,
                    MessagingException
Description copied from interface: Part
Write the Part content out to the provided OutputStream as a byte stream using an encoding appropriate to the Part content.

Specified by:
writeTo in interface Part
Parameters:
out - The target OutputStream.
Throws:
IOException
MessagingException

getHeader

public String[] getHeader(String name)
                   throws MessagingException
Description copied from interface: Part
Get all Headers for this header name. Returns null if no headers with the given name exist.

Specified by:
getHeader in interface Part
Parameters:
name - The target header name.
Returns:
An array of all matching header values, or null if the given header does not exist.
Throws:
MessagingException

getHeader

public String getHeader(String name,
                        String delimiter)
                 throws MessagingException
Specified by:
getHeader in interface MimePart
Throws:
MessagingException

setHeader

public void setHeader(String name,
                      String value)
               throws MessagingException
Description copied from interface: Part
Sets a value for the given header. This operation will replace all existing headers with the given name.

Specified by:
setHeader in interface Part
Parameters:
name - The name of the target header.
value - The new value for the indicated header.
Throws:
MessagingException

addHeader

public void addHeader(String name,
                      String value)
               throws MessagingException
Description copied from interface: Part
Add this value to the existing headers with the given name. This method does not replace any headers that may already exist.

Specified by:
addHeader in interface Part
Parameters:
name - The name of the target header.
value - The value to be added to the header set.
Throws:
MessagingException

removeHeader

public void removeHeader(String name)
                  throws MessagingException
Description copied from interface: Part
Remove all headers with the given name from the Part.

Specified by:
removeHeader in interface Part
Parameters:
name - The target header name used for removal.
Throws:
MessagingException

getAllHeaders

public Enumeration getAllHeaders()
                          throws MessagingException
Description copied from interface: Part
Return all headers as an Enumeration of Header objects.

Specified by:
getAllHeaders in interface Part
Returns:
An Enumeration containing all of the current Header objects.
Throws:
MessagingException

getMatchingHeaders

public Enumeration getMatchingHeaders(String[] name)
                               throws MessagingException
Description copied from interface: Part
Return all headers that match the list of names as an Enumeration of Header objects.

Specified by:
getMatchingHeaders in interface Part
Parameters:
name - An array of names of the desired headers.
Returns:
An Enumeration of Header objects containing the matching headers.
Throws:
MessagingException

getNonMatchingHeaders

public Enumeration getNonMatchingHeaders(String[] name)
                                  throws MessagingException
Description copied from interface: Part
Return an Enumeration of all Headers except those that match the names given in the exclusion list.

Specified by:
getNonMatchingHeaders in interface Part
Parameters:
name - An array of String header names that will be excluded from the return Enumeration set.
Returns:
An Enumeration of Headers containing all headers except for those named in the exclusion list.
Throws:
MessagingException

addHeaderLine

public void addHeaderLine(String line)
                   throws MessagingException
Specified by:
addHeaderLine in interface MimePart
Throws:
MessagingException

getAllHeaderLines

public Enumeration getAllHeaderLines()
                              throws MessagingException
Specified by:
getAllHeaderLines in interface MimePart
Throws:
MessagingException

getMatchingHeaderLines

public Enumeration getMatchingHeaderLines(String[] names)
                                   throws MessagingException
Specified by:
getMatchingHeaderLines in interface MimePart
Throws:
MessagingException

getNonMatchingHeaderLines

public Enumeration getNonMatchingHeaderLines(String[] names)
                                      throws MessagingException
Specified by:
getNonMatchingHeaderLines in interface MimePart
Throws:
MessagingException

updateHeaders

protected void updateHeaders()
                      throws MessagingException
Throws:
MessagingException

attachFile

public void attachFile(File file)
                throws IOException,
                       MessagingException
Attach a file to this body part from a File object.

Parameters:
file - The source File object.
Throws:
IOException
MessagingException

attachFile

public void attachFile(String file)
                throws IOException,
                       MessagingException
Attach a file to this body part from a file name.

Parameters:
file - The source file name.
Throws:
IOException
MessagingException

saveFile

public void saveFile(File file)
              throws IOException,
                     MessagingException
Save the body part content to a given target file.

Parameters:
file - The File object used to store the information.
Throws:
IOException
MessagingException

saveFile

public void saveFile(String file)
              throws IOException,
                     MessagingException
Save the body part content to a given target file.

Parameters:
file - The file name used to store the information.
Throws:
IOException
MessagingException


Copyright © 2003-2009 The Apache Software Foundation. All Rights Reserved.