Top Description Fields Constructors Methods
javax.mail.internet

public Class MimeMultipart

extends Multipart
Class Inheritance
Imports
javax.mail.*, javax.activation.*, java.util.*, java.io.*, com.sun.mail.util.LineOutputStream, .LineInputStream, .ASCIIUtility, .PropUtil

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.

The mail.mime.multipart.ignoreexistingboundaryparameter System property may be set to true to cause any boundary to be ignored and instead search for a boundary line in the message as with mail.mime.multipart.ignoremissingboundaryparameter.

Normally, when writing out a MimeMultipart that contains no body parts, or when trying to parse a multipart message with no body parts, a MessagingException is thrown. The MIME spec does not allow multipart content with no body parts. The mail.mime.multipart.allowempty System property may be set to true to override this behavior. When writing out such a MimeMultipart, a single empty part will be included. When reading such a multipart, a MimeMultipart will be created with no body parts.

Authors
John Mani, Bill Shannon, Max Spivak

Field Summary

Modifier and TypeField and Description
protected boolean
allowEmpty

Flag corresponding to the "mail.mime.multipart.allowempty" property, set in the initializeProperties method called from constructors and the parse method.

protected boolean
complete

Have we seen the final bounary line?

protected DataSource
ds

The DataSource supplying our InputStream.

protected boolean
ignoreExistingBoundaryParameter

Flag corresponding to the "mail.mime.multipart.ignoreexistingboundaryparameter" property, set in the initializeProperties method called from constructors and the parse method.

protected boolean
ignoreMissingBoundaryParameter

Flag corresponding to the "mail.mime.multipart.ignoremissingboundaryparameter" property, set in the initializeProperties method called from constructors and the parse method.

protected boolean
ignoreMissingEndBoundary

Flag corresponding to the "mail.mime.multipart.ignoremissingendboundary" property, set in the initializeProperties method called from constructors and the parse method.

protected boolean
parsed

Have we parsed the data from our InputStream yet?

protected String
preamble

The MIME multipart preamble text, the text that occurs before the first boundary line.

Inherited from javax.mail.Multipart:
contentTypeparentparts

Constructor Summary

AccessConstructor and Description
public
MimeMultipart()

Default constructor.

public
MimeMultipart(String
the MIME content subtype
subtype
)

Construct a MimeMultipart object of the given subtype.

public
MimeMultipart(BodyPart...
the body parts
parts
)

Construct a MimeMultipart object of the default "mixed" subtype, and with the given body parts.

public
MimeMultipart(String
the MIME content subtype
subtype
,
BodyPart...
the body parts
parts
)

Construct a MimeMultipart object of the given subtype and with the given body parts.

public
MimeMultipart(DataSource
DataSource, can be a MultipartDataSource
ds
)

Constructs a MimeMultipart object and its bodyparts from the given DataSource.

Method Summary

Modifier and TypeMethod and Description
public synchronized void
addBodyPart(BodyPart
The Part to be appended
part
)

Overrides javax.mail.Multipart.addBodyPart.

Adds a Part to the multipart.
public synchronized void
addBodyPart(BodyPart
The BodyPart to be inserted
part
,
int
Location where to insert the part
index
)

Overrides javax.mail.Multipart.addBodyPart.

Adds a BodyPart at position index.
private static boolean
allDashes(String s)

Is the string all dashes ('-')?

protected InternetHeaders

Returns:

an InternetHeaders object
createInternetHeaders
(InputStream
the InputStream to read the headers from
is
)

Create and return an InternetHeaders object that loads the headers from the given InputStream.

protected MimeBodyPart

Returns:

a MimeBodyPart
createMimeBodyPart
(InternetHeaders
the headers for the body part
headers
,
byte[]
the content of the body part
content
)

Create and return a MimeBodyPart object to represent a body part parsed from the InputStream.

protected MimeBodyPart

Returns:

a MimeBodyPart
createMimeBodyPart
(InputStream
InputStream containing the body part
is
)

Create and return a MimeBodyPart object to represent a body part parsed from the InputStream.

private MimeBodyPart
public synchronized BodyPart

Returns:

the Part
getBodyPart
(int
the index of the desired BodyPart
index
)

Overrides javax.mail.Multipart.getBodyPart.

Get the specified BodyPart.
public synchronized BodyPart

Returns:

the Part
getBodyPart
(String
the ContentID of the desired part
CID
)

Get the MimeBodyPart referred to by the given ContentID (CID).

public synchronized int

Returns:

number of parts
getCount
()

Overrides javax.mail.Multipart.getCount.

Return the number of enclosed BodyPart objects.
public synchronized String

Returns:

the preamble text, or null if no preamble
getPreamble
()

Get the preamble text, if any, that appears before the first body part of this multipart.

protected void
initializeProperties()

Initialize flags that control parsing behavior, based on System properties described above in the class documentation.

public synchronized boolean

Returns:

true if the final boundary line was seen
isComplete
()

Return true if the final boundary line for this multipart was seen.

protected synchronized void
parse()

Parse the InputStream from our DataSource, constructing the appropriate MimeBodyParts.

private static int

Returns:

-1 on EOF, otherwise number of bytes read
readFully
(InputStream
InputStream to read from
in
,
byte[]
buffer to read into
buf
,
int
offset in the buffer for first byte
off
,
int
number of bytes to read
len
)

Read data from the input stream to fill the buffer starting at the specified offset with the specified number of bytes.

public boolean

Returns:

true if part removed, false otherwise
removeBodyPart
(BodyPart
The part to remove
part
)

Overrides javax.mail.Multipart.removeBodyPart.

Remove the specified part from the multipart message.
public void
removeBodyPart(int
Index of the part to remove
index
)

Overrides javax.mail.Multipart.removeBodyPart.

Remove the part at specified location (starting from 0).
public synchronized void
setPreamble(String
the preamble text
preamble
)

Set the preamble text to be included before the first body part.

public synchronized void
setSubType(String
Subtype
subtype
)

Set the subtype.

private void
skipFully(InputStream in, long offset)

Skip the specified number of bytes, repeatedly calling the skip method as necessary.

protected synchronized void
updateHeaders()

Update headers.

public synchronized void
writeTo(OutputStream
the stream to write to
os
)

Implements abstract javax.mail.Multipart.writeTo.

Iterates through all the parts and outputs each MIME part separated by a boundary.
Inherited from javax.mail.Multipart:
getContentTypegetParentsetMultipartDataSourcesetParent

Field Detail

allowEmptyback to summary
protected boolean allowEmpty

Flag corresponding to the "mail.mime.multipart.allowempty" property, set in the initializeProperties method called from constructors and the parse method.

Since
JavaMail 1.5
completeback to summary
protected boolean complete

Have we seen the final bounary line?

Since
JavaMail 1.5
dsback to summary
protected DataSource ds

The DataSource supplying our InputStream.

ignoreExistingBoundaryParameterback to summary
protected boolean ignoreExistingBoundaryParameter

Flag corresponding to the "mail.mime.multipart.ignoreexistingboundaryparameter" property, set in the initializeProperties method called from constructors and the parse method.

Since
JavaMail 1.5
ignoreMissingBoundaryParameterback to summary
protected boolean ignoreMissingBoundaryParameter

Flag corresponding to the "mail.mime.multipart.ignoremissingboundaryparameter" property, set in the initializeProperties method called from constructors and the parse method.

Since
JavaMail 1.5
ignoreMissingEndBoundaryback to summary
protected boolean ignoreMissingEndBoundary

Flag corresponding to the "mail.mime.multipart.ignoremissingendboundary" property, set in the initializeProperties method called from constructors and the parse method.

Since
JavaMail 1.5
parsedback to summary
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.

preambleback to summary
protected String preamble

The MIME multipart preamble text, the text that occurs before the first boundary line.

Since
JavaMail 1.5

Constructor Detail

MimeMultipartback to summary
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.

MimeMultipartback to summary
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. Calls the initializeProperties method.

MimeBodyParts may be added later.

Parameters
subtype:String

the MIME content subtype

MimeMultipartback to summary
public MimeMultipart(BodyPart... parts) throws MessagingException

Construct a MimeMultipart object of the default "mixed" subtype, and with the given body parts. More body parts may be added later.

Parameters
parts:BodyPart[]

the body parts

Exceptions
MessagingException:
for failures
Since
JavaMail 1.5
MimeMultipartback to summary
public MimeMultipart(String subtype, BodyPart... parts) throws MessagingException

Construct a MimeMultipart object of the given subtype and with the given body parts. More body parts may be added later.

Parameters
subtype:String

the MIME content subtype

parts:BodyPart[]

the body parts

Exceptions
MessagingException:
for failures
Since
JavaMail 1.5
MimeMultipartback to summary
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.

Parameters
ds:DataSource

DataSource, can be a MultipartDataSource

Exceptions
MessagingException:
for other failures
ParseException:
for failures parsing the message

Method Detail

addBodyPartback to summary
public synchronized void addBodyPart(BodyPart part) throws MessagingException

Overrides javax.mail.Multipart.addBodyPart.

Adds a Part to the multipart. The BodyPart is appended to the list of existing Parts.

Parameters
part:BodyPart

The Part to be appended

Annotations
@Override
Exceptions
MessagingException:
for other failures
IllegalWriteException:
if the underlying implementation does not support modification of existing values
addBodyPartback to summary
public synchronized void addBodyPart(BodyPart part, int index) throws MessagingException

Overrides javax.mail.Multipart.addBodyPart.

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.

Parameters
part:BodyPart

The BodyPart to be inserted

index:int

Location where to insert the part

Annotations
@Override
Exceptions
MessagingException:
for other failures
IllegalWriteException:
if the underlying implementation does not support modification of existing values
allDashesback to summary
private static boolean allDashes(String s)

Is the string all dashes ('-')?

createInternetHeadersback to summary
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.

Parameters
is:InputStream

the InputStream to read the headers from

Returns:InternetHeaders

an InternetHeaders object

Exceptions
MessagingException:
for failures
Since
JavaMail 1.2
createMimeBodyPartback to summary
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.

Parameters
headers:InternetHeaders

the headers for the body part

content:byte[]

the content of the body part

Returns:MimeBodyPart

a MimeBodyPart

Exceptions
MessagingException:
for failures
Since
JavaMail 1.2
createMimeBodyPartback to summary
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.

Parameters
is:InputStream

InputStream containing the body part

Returns:MimeBodyPart

a MimeBodyPart

Exceptions
MessagingException:
for failures
Since
JavaMail 1.2
createMimeBodyPartIsback to summary
private MimeBodyPart createMimeBodyPartIs(InputStream is) throws MessagingException
getBodyPartback to summary
public synchronized BodyPart getBodyPart(int index) throws MessagingException

Overrides javax.mail.Multipart.getBodyPart.

Get the specified BodyPart. BodyParts are numbered starting at 0.

Parameters
index:int

the index of the desired BodyPart

Returns:BodyPart

the Part

Annotations
@Override
Exceptions
MessagingException:
if no such BodyPart exists
getBodyPartback to summary
public synchronized BodyPart getBodyPart(String CID) throws MessagingException

Get the MimeBodyPart referred to by the given ContentID (CID). Returns null if the part is not found.

Parameters
CID:String

the ContentID of the desired part

Returns:BodyPart

the Part

Exceptions
MessagingException:
for failures
getCountback to summary
public synchronized int getCount() throws MessagingException

Overrides javax.mail.Multipart.getCount.

Return the number of enclosed BodyPart objects.

Returns:int

number of parts

Annotations
@Override
Exceptions
MessagingException:

Doc from javax.mail.Multipart.getCount.

for failures

getPreambleback to summary
public synchronized 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.

Returns:String

the preamble text, or null if no preamble

Exceptions
MessagingException:
for failures
Since
JavaMail 1.4
initializePropertiesback to summary
protected void initializeProperties()

Initialize flags that control parsing behavior, based on System properties described above in the class documentation.

Since
JavaMail 1.5
isCompleteback to summary
public synchronized 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.)

Returns:boolean

true if the final boundary line was seen

Exceptions
MessagingException:
for failures
Since
JavaMail 1.4
parseback to summary
protected synchronized 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. The initializeProperties method is called before parsing the data.

Exceptions
MessagingException:
for other failures
ParseException:
for failures parsing the message
Since
JavaMail 1.2
readFullyback to summary
private static int readFully(InputStream in, byte[] buf, int off, int len) throws IOException

Read data from the input stream to fill the buffer starting at the specified offset with the specified number of bytes. If len is zero, return zero. If at EOF, return -1. Otherwise, return the number of bytes read. Call the read method on the input stream as many times as necessary to read len bytes.

Parameters
in:InputStream

InputStream to read from

buf:byte[]

buffer to read into

off:int

offset in the buffer for first byte

len:int

number of bytes to read

Returns:int

-1 on EOF, otherwise number of bytes read

Exceptions
IOException:
on I/O errors
removeBodyPartback to summary
public boolean removeBodyPart(BodyPart part) throws MessagingException

Overrides javax.mail.Multipart.removeBodyPart.

Remove the specified part from the multipart message. Shifts all the parts after the removed part down one.

Parameters
part:BodyPart

The part to remove

Returns:boolean

true if part removed, false otherwise

Annotations
@Override
Exceptions
MessagingException:
if no such Part exists
IllegalWriteException:
if the underlying implementation does not support modification of existing values
removeBodyPartback to summary
public void removeBodyPart(int index) throws MessagingException

Overrides javax.mail.Multipart.removeBodyPart.

Remove the part at specified location (starting from 0). Shifts all the parts after the removed part down one.

Parameters
index:int

Index of the part to remove

Annotations
@Override
Exceptions
MessagingException:
for other failures
IndexOutOfBoundsException:
if the given index is out of range.
IllegalWriteException:
if the underlying implementation does not support modification of existing values
setPreambleback to summary
public synchronized 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.

Parameters
preamble:String

the preamble text

Exceptions
MessagingException:
for failures
Since
JavaMail 1.4
setSubTypeback to summary
public synchronized 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".

Parameters
subtype:String

Subtype

Exceptions
MessagingException:
for failures
skipFullyback to summary
private void skipFully(InputStream in, long offset) throws IOException

Skip the specified number of bytes, repeatedly calling the skip method as necessary.

updateHeadersback to summary
protected synchronized 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.

Exceptions
MessagingException:
for failures
writeToback to summary
public synchronized void writeTo(OutputStream os) throws IOException, MessagingException

Implements abstract javax.mail.Multipart.writeTo.

Iterates through all the parts and outputs each MIME part separated by a boundary.

Parameters
os:OutputStream

Doc from javax.mail.Multipart.writeTo.

the stream to write to

Annotations
@Override
Exceptions
IOException:

Doc from javax.mail.Multipart.writeTo.

if an IO related exception occurs

MessagingException:

Doc from javax.mail.Multipart.writeTo.

for other failures