MimeMultipart (Java EE 5 SDK)

Java

Java EE 5 SDK

深圳电信培训中心.徐海蛟老师.


javax.mail.internet Class MimeMultipart

java.lang.Object
  extended by javax.mail.Multipart
      extended by javax.mail.internet.MimeMultipart

public class MimeMultipart
extends Multipart

The MimeMultipart class is an implementation of the abstract Multipart class that uses MIME conventions for the multipart data.

A MimeMultipart is obtained from a MimePart whose primary type is "multipart" (by invoking the part's getContent() method) or it can be created by a client as part of creating a new MimeMessage.

The default multipart subtype is "mixed". The other multipart subtypes, such as "alternative", "related", and so on, can be implemented as subclasses of MimeMultipart with additional methods to implement the additional semantics of that type of multipart content. The intent is that service providers, mail JavaBean writers and mail clients will write many such subclasses and their Command Beans, and will install them into the JavaBeans Activation Framework, so that any JavaMail implementation and its clients can transparently find and use these classes. Thus, a MIME multipart handler is treated just like any other type handler, thereby decoupling the process of providing multipart handlers from the JavaMail API. Lacking these additional MimeMultipart subclasses, all subtypes of MIME multipart data appear as MimeMultipart objects.

An application can directly construct a MIME multipart object of any subtype by using the MimeMultipart(String subtype) constructor. For example, to create a "multipart/alternative" object, use new MimeMultipart("alternative").

The mail.mime.multipart.ignoremissingendboundary property may be set to false to cause a MessagingException to be thrown if the multipart data does not end with the required end boundary line. If this property is set to true or not set, missing end boundaries are not considered an error and the final body part ends at the end of the data.

The mail.mime.multipart.ignoremissingboundaryparameter System property may be set to false to cause a MessagingException to be thrown if the Content-Type of the MimeMultipart does not include a boundary parameter. If this property is set to true or not set, the multipart parsing code will look for a line that looks like a bounary line and use that as the boundary separating the parts.

版本:
1.48, 07/05/15
作者:
John Mani, Bill Shannon, Max Spivak

字段摘要
protected  DataSource ds
          The DataSource supplying our InputStream.
protected  boolean parsed
          Have we parsed the data from our InputStream yet?
 
Fields inherited from class javax.mail.Multipart
contentType, parent, parts
 
构造器摘要
MimeMultipart()
          Default constructor.
MimeMultipart(DataSource ds)
          Constructs a MimeMultipart object and its bodyparts from the given DataSource.
MimeMultipart(String subtype)
          Construct a MimeMultipart object of the given subtype.
 
方法摘要
 void addBodyPart(BodyPart part)
          Adds a Part to the multipart.
 void addBodyPart(BodyPart part, int index)
          Adds a BodyPart at position index.
protected  InternetHeaders createInternetHeaders(InputStream is)
          Create and return an InternetHeaders object that loads the headers from the given InputStream.
protected  MimeBodyPart createMimeBodyPart(InputStream is)
          Create and return a MimeBodyPart object to represent a body part parsed from the InputStream.
protected  MimeBodyPart createMimeBodyPart(InternetHeaders headers, byte[] content)
          Create and return a MimeBodyPart object to represent a body part parsed from the InputStream.
 BodyPart getBodyPart(int index)
          Get the specified BodyPart.
 BodyPart getBodyPart(String CID)
          Get the MimeBodyPart referred to by the given ContentID (CID).
 int getCount()
          Return the number of enclosed BodyPart objects.
 String getPreamble()
          Get the preamble text, if any, that appears before the first body part of this multipart.
 boolean isComplete()
          Return true if the final boundary line for this multipart was seen.
protected  void parse()
          Parse the InputStream from our DataSource, constructing the appropriate MimeBodyParts.
 boolean removeBodyPart(BodyPart part)
          Remove the specified part from the multipart message.
 void removeBodyPart(int index)
          Remove the part at specified location (starting from 0).
 void setPreamble(String preamble)
          Set the preamble text to be included before the first body part.
 void setSubType(String subtype)
          Set the subtype.
protected  void updateHeaders()
          Update headers.
 void writeTo(OutputStream os)
          Iterates through all the parts and outputs each MIME part separated by a boundary.
 
类方法继承 javax.mail.Multipart
getContentType, getParent, setMultipartDataSource, setParent
 
类方法继承 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

字段详细信息

ds

protected DataSource ds
The DataSource supplying our InputStream.


parsed

protected boolean parsed
Have we parsed the data from our InputStream yet? Defaults to true; set to false when our constructor is given a DataSource with an InputStream that we need to parse.

构造器详细信息

MimeMultipart

public MimeMultipart()
Default constructor. An empty MimeMultipart object is created. Its content type is set to "multipart/mixed". A unique boundary string is generated and this string is setup as the "boundary" parameter for the contentType field.

MimeBodyParts may be added later.


MimeMultipart

public MimeMultipart(String subtype)
Construct a MimeMultipart object of the given subtype. A unique boundary string is generated and this string is setup as the "boundary" parameter for the contentType field.

MimeBodyParts may be added later.


MimeMultipart

public MimeMultipart(DataSource ds)
              throws MessagingException
Constructs a MimeMultipart object and its bodyparts from the given DataSource.

This constructor handles as a special case the situation where the given DataSource is a MultipartDataSource object. In this case, this method just invokes the superclass (i.e., Multipart) constructor that takes a MultipartDataSource object.

Otherwise, the DataSource is assumed to provide a MIME multipart byte stream. The parsed flag is set to false. When the data for the body parts are needed, the parser extracts the "boundary" parameter from the content type of this DataSource, skips the 'preamble' and reads bytes till the terminating boundary and creates MimeBodyParts for each part of the stream.

参数:
ds - DataSource, can be a MultipartDataSource
抛出异常:
MessagingException

方法详细信息

setSubType

public void setSubType(String subtype)
                throws MessagingException
Set the subtype. This method should be invoked only on a new MimeMultipart object created by the client. The default subtype of such a multipart object is "mixed".

参数:
subtype - Subtype
抛出异常:
MessagingException

getCount

public int getCount()
             throws MessagingException
Return the number of enclosed BodyPart objects.

重写:
getCount in class Multipart
返回:
number of parts
抛出异常:
MessagingException
另请参见:
Multipart.parts

getBodyPart

public BodyPart getBodyPart(int index)
                     throws MessagingException
Get the specified BodyPart. BodyParts are numbered starting at 0.

重写:
getBodyPart in class Multipart
参数:
index - the index of the desired BodyPart
返回:
the Part
抛出异常:
MessagingException - if no such BodyPart exists

getBodyPart

public BodyPart getBodyPart(String CID)
                     throws MessagingException
Get the MimeBodyPart referred to by the given ContentID (CID). Returns null if the part is not found.

参数:
CID - the ContentID of the desired part
返回:
the Part
抛出异常:
MessagingException

removeBodyPart

public boolean removeBodyPart(BodyPart part)
                       throws MessagingException
Remove the specified part from the multipart message. Shifts all the parts after the removed part down one.

重写:
removeBodyPart in class Multipart
参数:
part - The part to remove
返回:
true if part removed, false otherwise
抛出异常:
MessagingException - if no such Part exists
IllegalWriteException - if the underlying implementation does not support modification of existing values

removeBodyPart

public void removeBodyPart(int index)
                    throws MessagingException
Remove the part at specified location (starting from 0). Shifts all the parts after the removed part down one.

重写:
removeBodyPart in class Multipart
参数:
index - Index of the part to remove
抛出异常:
MessagingException
IndexOutOfBoundsException - if the given index is out of range.
IllegalWriteException - if the underlying implementation does not support modification of existing values

addBodyPart

public void addBodyPart(BodyPart part)
                 throws MessagingException
Adds a Part to the multipart. The BodyPart is appended to the list of existing Parts.

重写:
addBodyPart in class Multipart
参数:
part - The Part to be appended
抛出异常:
MessagingException
IllegalWriteException - if the underlying implementation does not support modification of existing values

addBodyPart

public void addBodyPart(BodyPart part,
                        int index)
                 throws MessagingException
Adds a BodyPart at position index. If index is not the last one in the list, the subsequent parts are shifted up. If index is larger than the number of parts present, the BodyPart is appended to the end.

重写:
addBodyPart in class Multipart
参数:
part - The BodyPart to be inserted
index - Location where to insert the part
抛出异常:
MessagingException
IllegalWriteException - if the underlying implementation does not support modification of existing values

isComplete

public boolean isComplete()
                   throws MessagingException
Return true if the final boundary line for this multipart was seen. When parsing multipart content, this class will (by default) terminate parsing with no error if the end of input is reached before seeing the final multipart boundary line. In such a case, this method will return false. (If the System property "mail.mime.multipart.ignoremissingendboundary" is set to false, parsing such a message will instead throw a MessagingException.)

返回:
true if the final boundary line was seen
抛出异常:
MessagingException
从以下版本开始:
JavaMail 1.4

getPreamble

public String getPreamble()
                   throws MessagingException
Get the preamble text, if any, that appears before the first body part of this multipart. Some protocols, such as IMAP, will not allow access to the preamble text.

返回:
the preamble text, or null if no preamble
抛出异常:
MessagingException
从以下版本开始:
JavaMail 1.4

setPreamble

public void setPreamble(String preamble)
                 throws MessagingException
Set the preamble text to be included before the first body part. Applications should generally not include any preamble text. In some cases it may be helpful to include preamble text with instructions for users of pre-MIME software. The preamble text should be complete lines, including newlines.

参数:
preamble - the preamble text
抛出异常:
MessagingException
从以下版本开始:
JavaMail 1.4

updateHeaders

protected void updateHeaders()
                      throws MessagingException
Update headers. The default implementation here just calls the updateHeaders method on each of its children BodyParts.

Note that the boundary parameter is already set up when a new and empty MimeMultipart object is created.

This method is called when the saveChanges method is invoked on the Message object containing this Multipart. This is typically done as part of the Message send process, however note that a client is free to call it any number of times. So if the header updating process is expensive for a specific MimeMultipart subclass, then it might itself want to track whether its internal state actually did change, and do the header updating only if necessary.

抛出异常:
MessagingException

writeTo

public void writeTo(OutputStream os)
             throws IOException,
                    MessagingException
Iterates through all the parts and outputs each MIME part separated by a boundary.

规范说明:
writeTo in class Multipart
抛出异常:
IOException - if an IO related exception occurs
MessagingException

parse

protected void parse()
              throws MessagingException
Parse the InputStream from our DataSource, constructing the appropriate MimeBodyParts. The parsed flag is set to true, and if true on entry nothing is done. This method is called by all other methods that need data for the body parts, to make sure the data has been parsed.

抛出异常:
MessagingException
从以下版本开始:
JavaMail 1.2

createInternetHeaders

protected InternetHeaders createInternetHeaders(InputStream is)
                                         throws MessagingException
Create and return an InternetHeaders object that loads the headers from the given InputStream. Subclasses can override this method to return a subclass of InternetHeaders, if necessary. This implementation simply constructs and returns an InternetHeaders object.

参数:
is - the InputStream to read the headers from
抛出异常:
MessagingException
从以下版本开始:
JavaMail 1.2

createMimeBodyPart

protected MimeBodyPart createMimeBodyPart(InternetHeaders headers,
                                          byte[] content)
                                   throws MessagingException
Create and return a MimeBodyPart object to represent a body part parsed from the InputStream. Subclasses can override this method to return a subclass of MimeBodyPart, if necessary. This implementation simply constructs and returns a MimeBodyPart object.

参数:
headers - the headers for the body part
content - the content of the body part
抛出异常:
MessagingException
从以下版本开始:
JavaMail 1.2

createMimeBodyPart

protected MimeBodyPart createMimeBodyPart(InputStream is)
                                   throws MessagingException
Create and return a MimeBodyPart object to represent a body part parsed from the InputStream. Subclasses can override this method to return a subclass of MimeBodyPart, if necessary. This implementation simply constructs and returns a MimeBodyPart object.

参数:
is - InputStream containing the body part
抛出异常:
MessagingException
从以下版本开始:
JavaMail 1.2

Java EE 5 SDK

深圳电信培训中心.徐海蛟老师.


提交错误或意见

版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。深圳电信培训中心.徐海蛟老师教学参考.