Module jakarta.mail
Package jakarta.mail

Interface Part

  • All Known Subinterfaces:
    MimePart
    All Known Implementing Classes:
    BodyPart, Message, MimeBodyPart, MimeMessage, PreencodedMimeBodyPart

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

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

    Attributes:

    The Jakarta Mail 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:

    • As a DataHandler - using the getDataHandler() method. The "content" of a Part is also available through a jakarta.activation.DataHandler object. The DataHandler object allows clients to discover the operations available on the content, and to instantiate the appropriate component to perform those operations.
    • As an input stream - using the getInputStream() method. Any mail-specific encodings are decoded before this stream is returned.
    • As a Java object - using the getContent() method. This method returns the "content" as a Java object. The returned object is of course dependent on the content itself. In particular, a "multipart" Part's content is always a Multipart or subclass thereof. That is, getContent() on a "multipart" type Part will always return a Multipart (or subclass) object.
    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

      Fields 
      Modifier and Type Field Description
      static java.lang.String ATTACHMENT
      This part should be presented as an attachment.
      static java.lang.String INLINE
      This part should be presented inline.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void addHeader​(java.lang.String header_name, java.lang.String header_value)
      Add this value to the existing values for this header_name.
      java.util.Enumeration<Header> getAllHeaders()
      Return all the headers from this part as an Enumeration of Header objects.
      java.lang.Object getContent()
      Return the content as a Java object.
      java.lang.String getContentType()
      Returns the Content-Type of the content of this part.
      jakarta.activation.DataHandler getDataHandler()
      Return a DataHandler for the content within this part.
      java.lang.String getDescription()
      Return a description String for this part.
      java.lang.String getDisposition()
      Return the disposition of this part.
      java.lang.String getFileName()
      Get the filename associated with this part, if possible.
      java.lang.String[] getHeader​(java.lang.String header_name)
      Get all the headers for this header name.
      java.io.InputStream getInputStream()
      Return an input stream for this part's "content".
      int getLineCount()
      Return the number of lines in the content of this part.
      java.util.Enumeration<Header> getMatchingHeaders​(java.lang.String[] header_names)
      Return matching headers from this part as an Enumeration of Header objects.
      java.util.Enumeration<Header> getNonMatchingHeaders​(java.lang.String[] header_names)
      Return non-matching headers from this envelope as an Enumeration of Header objects.
      int getSize()
      Return the size of the content of this part in bytes.
      boolean isMimeType​(java.lang.String mimeType)
      Is this Part of the specified MIME type? This method compares only the primaryType and subType.
      void removeHeader​(java.lang.String header_name)
      Remove all headers with this name.
      void setContent​(Multipart mp)
      This method sets the given Multipart object as this message's content.
      void setContent​(java.lang.Object obj, java.lang.String type)
      A convenience method for setting this part's content.
      void setDataHandler​(jakarta.activation.DataHandler dh)
      This method provides the mechanism to set this part's content.
      void setDescription​(java.lang.String description)
      Set a description String for this part.
      void setDisposition​(java.lang.String disposition)
      Set the disposition of this part.
      void setFileName​(java.lang.String filename)
      Set the filename associated with this part, if possible.
      void setHeader​(java.lang.String header_name, java.lang.String header_value)
      Set the value for this header_name.
      void setText​(java.lang.String text)
      A convenience method that sets the given String as this part's content with a MIME type of "text/plain".
      void writeTo​(java.io.OutputStream os)
      Output a bytestream for this Part.
    • Method Detail

      • getSize

        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:
        size of content in bytes
        Throws:
        MessagingException - for failures
      • getLineCount

        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:
        number of lines in the content.
        Throws:
        MessagingException - for failures
      • getContentType

        java.lang.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:
        The ContentType of this part
        Throws:
        MessagingException - for failures
        See Also:
        DataHandler
      • isMimeType

        boolean isMimeType​(java.lang.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 - the MIME type to test
        Returns:
        true if this part is of the specified type
        Throws:
        MessagingException - for failures
      • getDisposition

        java.lang.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:
        disposition of this part, or null if unknown
        Throws:
        MessagingException - for failures
        See Also:
        ATTACHMENT, INLINE, getFileName()
      • getDescription

        java.lang.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:
        description of this part
        Throws:
        MessagingException - for failures
      • setDescription

        void setDescription​(java.lang.String description)
                     throws MessagingException
        Set a description String for this part. This typically associates some descriptive information with this part.
        Parameters:
        description - description of this part
        Throws:
        IllegalWriteException - if the underlying implementation does not support modification of this header
        java.lang.IllegalStateException - if this Part is obtained from a READ_ONLY folder
        MessagingException - for other failures
      • getFileName

        java.lang.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:
        Filename to associate with this part
        Throws:
        MessagingException - for failures
      • setFileName

        void setFileName​(java.lang.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 - Filename to associate with this part
        Throws:
        IllegalWriteException - if the underlying implementation does not support modification of this header
        java.lang.IllegalStateException - if this Part is obtained from a READ_ONLY folder
        MessagingException - for other failures
      • getInputStream

        java.io.InputStream getInputStream()
                                    throws java.io.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:
        an InputStream
        Throws:
        java.io.IOException - this is typically thrown by the DataHandler. Refer to the documentation for jakarta.activation.DataHandler for more details.
        MessagingException - for other failures
        See Also:
        getDataHandler(), DataHandler.getInputStream()
      • getDataHandler

        jakarta.activation.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 for the content
        Throws:
        MessagingException - for failures
      • getContent

        java.lang.Object getContent()
                             throws java.io.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
        Throws:
        java.io.IOException - this is typically thrown by the DataHandler. Refer to the documentation for jakarta.activation.DataHandler for more details.
        MessagingException - for other failures
        See Also:
        DataHandler.getContent()
      • setDataHandler

        void setDataHandler​(jakarta.activation.DataHandler dh)
                     throws MessagingException
        This method provides the mechanism to set this part's content. The DataHandler wraps around the actual content.
        Parameters:
        dh - The DataHandler for the content.
        Throws:
        IllegalWriteException - if the underlying implementation does not support modification of existing values
        java.lang.IllegalStateException - if this Part is obtained from a READ_ONLY folder
        MessagingException - for other failures
      • setContent

        void setContent​(java.lang.Object obj,
                        java.lang.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 Jakarta Mail 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 - A java object.
        type - MIME type of this object.
        Throws:
        IllegalWriteException - if the underlying implementation does not support modification of existing values
        java.lang.IllegalStateException - if this Part is obtained from a READ_ONLY folder
        MessagingException - for other failures
      • setText

        void setText​(java.lang.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 - The text that is the Message's content.
        Throws:
        IllegalWriteException - if the underlying implementation does not support modification of existing values
        java.lang.IllegalStateException - if this Part is obtained from a READ_ONLY folder
        MessagingException - for other failures
      • setContent

        void setContent​(Multipart mp)
                 throws MessagingException
        This method sets the given Multipart object as this message's content.
        Parameters:
        mp - The multipart object that is the Message's content
        Throws:
        IllegalWriteException - if the underlying implementation does not support modification of existing values
        java.lang.IllegalStateException - if this Part is obtained from a READ_ONLY folder
        MessagingException - for other failures
      • writeTo

        void writeTo​(java.io.OutputStream os)
              throws java.io.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 - the stream to write to
        Throws:
        java.io.IOException - if an error occurs writing to the stream or if an error is generated by the jakarta.activation layer.
        MessagingException - if an error occurs fetching the data to be written
        See Also:
        DataHandler.writeTo(java.io.OutputStream)
      • getHeader

        java.lang.String[] getHeader​(java.lang.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 - the name of this header
        Returns:
        the value fields for all headers with this name
        Throws:
        MessagingException - for failures
      • setHeader

        void setHeader​(java.lang.String header_name,
                       java.lang.String header_value)
                throws MessagingException
        Set the value for this header_name. Replaces all existing header values with this new value.
        Parameters:
        header_name - the name of this header
        header_value - the value for this header
        Throws:
        IllegalWriteException - if the underlying implementation does not support modification of existing values
        java.lang.IllegalStateException - if this Part is obtained from a READ_ONLY folder
        MessagingException - for other failures
      • addHeader

        void addHeader​(java.lang.String header_name,
                       java.lang.String header_value)
                throws MessagingException
        Add this value to the existing values for this header_name.
        Parameters:
        header_name - the name of this header
        header_value - the value for this header
        Throws:
        IllegalWriteException - if the underlying implementation does not support modification of existing values
        java.lang.IllegalStateException - if this Part is obtained from a READ_ONLY folder
        MessagingException - for other failures
      • removeHeader

        void removeHeader​(java.lang.String header_name)
                   throws MessagingException
        Remove all headers with this name.
        Parameters:
        header_name - the name of this header
        Throws:
        IllegalWriteException - if the underlying implementation does not support modification of existing values
        java.lang.IllegalStateException - if this Part is obtained from a READ_ONLY folder
        MessagingException - for other failures
      • getAllHeaders

        java.util.Enumeration<Header> getAllHeaders()
                                             throws MessagingException
        Return all the headers from this part as an Enumeration of Header objects.
        Returns:
        enumeration of Header objects
        Throws:
        MessagingException - for failures
      • getMatchingHeaders

        java.util.Enumeration<Header> getMatchingHeaders​(java.lang.String[] header_names)
                                                  throws MessagingException
        Return matching headers from this part as an Enumeration of Header objects.
        Parameters:
        header_names - the headers to match
        Returns:
        enumeration of Header objects
        Throws:
        MessagingException - for failures
      • getNonMatchingHeaders

        java.util.Enumeration<Header> getNonMatchingHeaders​(java.lang.String[] header_names)
                                                     throws MessagingException
        Return non-matching headers from this envelope as an Enumeration of Header objects.
        Parameters:
        header_names - the headers to not match
        Returns:
        enumeration of Header objects
        Throws:
        MessagingException - for failures