MimeBodyPart (Java EE 5)

Java EE API


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

Extends: BodyPart
Implements: MimePart

此类表示 MIME 正文部分。它实现 BodyPart 抽象类和 MimePart 接口。MimeBodyPart 包含在 MimeMultipart 对象中。

MimeBodyPart 使用 InternetHeaders 类解析和存储该正文部分的头。


有关 RFC 822 和 MIME 头的注意事项

RFC 822 头字段只能包含 US-ASCII 字符。通过解码非 ASCII 字符,MIME 允许这些字符存在于特定头的特定部分。RFC 2047 指定实现此操作的规则。此包提供的 MimeUtility 类可以用来实现这一点。setHeaderaddHeaderaddHeaderLine 方法的调用者负责针对特定的头强制执行 MIME 要求。此外,如果这些头字段超过了传输的行长度限制(SMTP 1000 字节),则发送前必须进行折叠(包装)。收到的头可能已经进行了折叠。应用程序负责根据需要折叠和展开头。

英文文档:

This class represents a MIME body part. It implements the BodyPart abstract class and the MimePart interface. MimeBodyParts are contained in MimeMultipart objects.

MimeBodyPart uses the InternetHeaders class to parse and store the headers of that body part.


A note on RFC 822 and MIME headers

RFC 822 header fields must contain only US-ASCII characters. MIME allows non ASCII characters to be present in certain portions of certain headers, by encoding those characters. RFC 2047 specifies the rules for doing this. The MimeUtility class provided in this package can be used to to achieve this. Callers of the setHeader, addHeader, and addHeaderLine methods are responsible for enforcing the MIME requirements for the specified headers. In addition, these header fields must be folded (wrapped) before being sent if they exceed the line length limitation for the transport (1000 bytes for SMTP). Received headers may have been folded. The application is responsible for folding and unfolding headers as appropriate.

Author:
John Mani, Bill Shannon, Kanwar Oberoi
See Also:
Part, MimePart, MimeUtility

Field Summary
protected  byte[]
protected  InputStream
protected  DataHandler
protected  InternetHeaders
 
Fields inherited from class javax.mail.BodyPart
 
Fields inherited from interface javax.mail.Part
 
Constructor Summary
 
Method Summary
 void
 void
 void
 void
 Enumeration
 Enumeration
 Object
 String
 String[]
 String
protected  InputStream
 String
 DataHandler
 String
 String
 String
 String
 String[]
 String
 InputStream
 int
 Enumeration
 Enumeration
 Enumeration
 Enumeration
 InputStream
 int
 boolean
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
 void
protected  void
 void
 
Methods inherited from class javax.mail.BodyPart
 
Methods inherited from class java.lang.Object
 

Field Detail

英文文档:

dh

protected DataHandler dh
The DataHandler object representing this Part's content.


英文文档:

content

protected byte[] content
Byte array that holds the bytes of the content of this Part.


英文文档:

contentStream

protected InputStream contentStream
If the data for this body part was supplied by an InputStream that implements the SharedInputStream interface, contentStream is another such stream representing the content of this body part. In this case, content will be null.

Since:
JavaMail 1.2


英文文档:

headers

protected InternetHeaders headers
The InternetHeaders object that stores all the headers of this body part.

Constructor Detail

public MimeBodyPart()
创建一个空 MimeBodyPart 对象。此正文部分可以由构造 multipart 消息的客户端填充。
英文文档:

MimeBodyPart

public MimeBodyPart()
An empty MimeBodyPart object is created. This body part maybe filled in by a client constructing a multipart message.


public MimeBodyPart(java.io.InputStream is) throws MessagingException
通过读取和解析指定输入流中的数据来构造 MimeBodyPart。解析器将使用给定输入流末尾之前的数据。输入流必须从有效 MIME 正文部分的开头开始,至该正文部分的末尾结束。

注意,分隔正文部分的“边界”字符串不能包含在输入流中。这是因为 MimeMultipart 解析器将从 multipart 流中提取每个正文部分的字节,并将它们放入此构造方法中,不带分界符字符串。

is 正文部分 InputStream

英文文档:

MimeBodyPart

public MimeBodyPart(InputStream is)
             throws MessagingException
Constructs a MimeBodyPart by reading and parsing the data from the specified input stream. The parser consumes data till the end of the given input stream. The input stream must start at the beginning of a valid MIME body part and must terminate at the end of that body part.

Note that the "boundary" string that delimits body parts must not be included in the input stream. The intention is that the MimeMultipart parser will extract each body part's bytes from a multipart stream and feed them into this constructor, without the delimiter strings.

Parameters:
is - the body part Input Stream
Throws:
MessagingException


public MimeBodyPart(InternetHeaders headers, byte[] content) throws MessagingException
使用给定的头和内容字节构造 MimeBodyPart。

供提供者使用。

headers 此部分的头
content 表示此部分正文的字节。

英文文档:

MimeBodyPart

public MimeBodyPart(InternetHeaders headers,
                    byte[] content)
             throws MessagingException
Constructs a MimeBodyPart using the given header and content bytes.

Used by providers.

Parameters:
headers - The header of this part
content - bytes representing the body of this part.
Throws:
MessagingException

Method Detail

public int getSize() throws MessagingException
返回此正文部分的内容大小(以字节为单位)。如果无法确定大小,则返回 -1。

注意,此数值可能不是内容大小的准确度量,可能没有考虑内容的传输编码。

此实现返回 content 数组(如果非 null)的大小;如果 contentStream 非 null,且 available 方法返回一个正数,则它返回该数作为该大小。否则,它返回 -1。

return 以字节为单位的大小;如果未知,则返回 -1。

英文文档:

getSize

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

Note that this number may not be an exact measure of the content size and may or may not account for any transfer encoding of the content.

This implementation returns the size of the content array (if not null), or, if contentStream is not null, and the available method returns a positive number, it returns that number as the size. Otherwise, it returns -1.

Specified by:
getSize in interface Part
Returns:
size in bytes, or -1 if not known
Throws:
MessagingException

public int getLineCount() throws MessagingException
返回此 Part 内容的行数。如果无法确定此数值,则返回 -1。

注意,此数值可能不是内容长度的准确度量,可能没有考虑内容的传输编码。

此实现返回 -1。

return 行数;如果未知,则返回 -1。

英文文档:

getLineCount

public int getLineCount()
                 throws MessagingException
Return the number of lines for the content of this Part. Return -1 if this 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.

This implementation returns -1.

Specified by:
getLineCount in interface Part
Returns:
number of lines, or -1 if not known
Throws:
MessagingException

public String getContentType() throws MessagingException
返回 RFC 822 "Content-Type" 头字段的值。它表示此正文部分内容的内容类型。此值不得为 null。如果此字段不可用,则应该返回 "text/plain"。

此实现使用 getHeader(name) 获取必需的头字段。

return 此正文部分的 Content-Type。

英文文档:

getContentType

public String getContentType()
                      throws MessagingException
Returns the value of the RFC 822 "Content-Type" header field. This represents the content type of the content of this body part. This value must not be null. If this field is unavailable, "text/plain" should be returned.

This implementation uses getHeader(name) to obtain the requisite header field.

Specified by:
getContentType in interface Part
Returns:
Content-Type of this body part
Throws:
MessagingException
See Also:
DataHandler

public boolean isMimeType(String mimeType) throws MessagingException
此 Part 是指定的 MIME 类型吗?此方法仅比较 primaryTypesubType。忽略内容类型参数。

例如,当比较内容类型的 "text/plain" 部分与 "text/plain; charset=foobar" 时,此方法将返回 true

如果 mimeTypesubType 是特殊字符 '*',则比较过程中将忽略 subtype。

英文文档:

isMimeType

public boolean isMimeType(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.

Specified by:
isMimeType in interface Part
Throws:
MessagingException

public String getDisposition() throws MessagingException
返回 "Content-Disposition" 头字段的值。这表示此部分的 disposition。disposition 描述各部分应该如何展示给用户。

如果 Content-Disposition 字段不可用,则返回 null。

此实现使用 getHeader(name) 获取必需的头字段。

See also headers

英文文档:

getDisposition

public String getDisposition()
                      throws MessagingException
Returns the value of the "Content-Disposition" header field. This represents the disposition of this part. The disposition describes how the part should be presented to the user.

If the Content-Disposition field is unavailable, null is returned.

This implementation uses getHeader(name) to obtain the requisite header field.

Specified by:
getDisposition in interface Part
Returns:
disposition of this part, or null if unknown
Throws:
MessagingException
See Also:
headers

public void setDisposition(String disposition) throws MessagingException
设置此正文部分的 "Content-Disposition" 头字段。如果 disposition 为 null,则移除所有现有的 "Content-Disposition" 头字段。
ThrowsIllegalWriteException: 如果底层实现不支持修改
ThrowsIllegalStateException: 如果此正文部分可从 READ_ONLY 文件夹获取。
英文文档:

setDisposition

public void setDisposition(String disposition)
                    throws MessagingException
Set the "Content-Disposition" header field of this body part. If the disposition is null, any existing "Content-Disposition" header field is removed.

Specified by:
setDisposition in interface Part
Parameters:
disposition - disposition of this part
Throws:
IllegalWriteException - if the underlying implementation does not support modification
IllegalStateException - if this body part is obtained from a READ_ONLY folder.
MessagingException
See Also:
Part.ATTACHMENT, Part.INLINE, Part.setFileName(java.lang.String)

public String getEncoding() throws MessagingException
返回 "Content-Transfer-Encoding" 头字段中的内容传输编码。如果头不可用或其值不存在,则返回 null

此实现使用 getHeader(name) 获取必需的头字段。

See also headers

英文文档:

getEncoding

public String getEncoding()
                   throws MessagingException
Returns the content transfer encoding from the "Content-Transfer-Encoding" header field. Returns null if the header is unavailable or its value is absent.

This implementation uses getHeader(name) to obtain the requisite header field.

Specified by:
getEncoding in interface MimePart
Returns:
content-transfer-encoding
Throws:
MessagingException
See Also:
headers

public String getContentID() throws MessagingException
返回 "Content-ID" 头字段的值。如果字段不可用或其值不存在,则返回 null

此实现使用 getHeader(name) 获取必需的头字段。

英文文档:

getContentID

public String getContentID()
                    throws MessagingException
Returns the value of the "Content-ID" header field. Returns null if the field is unavailable or its value is absent.

This implementation uses getHeader(name) to obtain the requisite header field.

Specified by:
getContentID in interface MimePart
Returns:
content-ID
Throws:
MessagingException

public void setContentID(String cid) throws MessagingException
设置此正文部分的 "Content-ID" 头字段。如果 cid 参数为 null,则移除所有现有的 "Content-ID"。
ThrowsIllegalWriteException: 如果底层实现不支持修改
ThrowsIllegalStateException: 如果此正文部分可从 READ_ONLY 文件夹获取。
ThrowsMessagingException:
since
JavaMail 1.3
英文文档:

setContentID

public void setContentID(String cid)
                  throws MessagingException
Set the "Content-ID" header field of this body part. If the cid parameter is null, any existing "Content-ID" is removed.

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

public String getContentMD5() throws MessagingException
返回 "Content-MD5" 头字段的值。如果此字段不可用或其值不存在,则返回 null

此实现使用 getHeader(name) 获取必需的头字段。

英文文档:

getContentMD5

public String getContentMD5()
                     throws MessagingException
Return the value of the "Content-MD5" header field. Returns null if this field is unavailable or its value is absent.

This implementation uses getHeader(name) to obtain the requisite header field.

Specified by:
getContentMD5 in interface MimePart
Returns:
content-MD5
Throws:
MessagingException

public void setContentMD5(String md5) throws MessagingException
设置此正文部分的 "Content-MD5" 头字段。
ThrowsIllegalWriteException: 如果底层实现不支持修改
ThrowsIllegalStateException: 如果此正文部分可从 READ_ONLY 文件夹获取。
英文文档:

setContentMD5

public void setContentMD5(String md5)
                   throws MessagingException
Set the "Content-MD5" header field of this body part.

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

public String[] getContentLanguage() throws MessagingException
获取在此 MimePart 的 Content-Language 头中指定的语言。Content-Language 头由 RFC 1766 定义。如果此头不可用或其值不存在,则返回 null

此实现使用 getHeader(name) 获取必需的头字段。

英文文档:

getContentLanguage

public String[] getContentLanguage()
                            throws MessagingException
Get the languages specified in the Content-Language header of this MimePart. The Content-Language header is defined by RFC 1766. Returns null if this header is not available or its value is absent.

This implementation uses getHeader(name) to obtain the requisite header field.

Specified by:
getContentLanguage in interface MimePart
Throws:
MessagingException

public void setContentLanguage(String[] languages) throws MessagingException
设置此 MimePart 的 Content-Language 头。Content-Language 头由 RFC 1766 定义。
languages 语言标记数组
英文文档:

setContentLanguage

public void setContentLanguage(String[] languages)
                        throws MessagingException
Set the Content-Language header of this MimePart. The Content-Language header is defined by RFC 1766.

Specified by:
setContentLanguage in interface MimePart
Parameters:
languages - array of language tags
Throws:
IllegalWriteException - if the underlying implementation does not support modification
MessagingException

public String getDescription() throws MessagingException
返回此正文部分的 "Content-Description" 头字段。这通常包括一些与此部分关联的描述性信息。如果此字段不可用或其值不存在,则返回 null。

如果 Content-Description 字段根据 RFC 2047 进行编码,则它将被解码并转换为 Unicode。如果解码或转换失败,则原数据按原样返回。

此实现使用 getHeader(name) 获取必需的头字段。

return 内容描述

英文文档:

getDescription

public String getDescription()
                      throws MessagingException
Returns the "Content-Description" header field of this body part. This typically associates some descriptive information with this part. Returns null if this field is unavailable or its value is absent.

If the Content-Description field is encoded as per RFC 2047, it is decoded and converted into Unicode. If the decoding or conversion fails, the raw data is returned as is.

This implementation uses getHeader(name) to obtain the requisite header field.

Specified by:
getDescription in interface Part
Returns:
content description
Throws:
MessagingException

public void setDescription(String description) throws MessagingException
设置此正文部分的 "Content-Description" 头字段。如果 description 参数为 null,则移除所有现有的 "Content-Description" 字段。

如果 description 包含非 US-ASCII 字符,则它将使用平台的默认字符集进行编码。如果 description 仅包含 US-ASCII 字符,则不进行编码,它将按原样使用。

注意,如果字符集编码处理失败,则抛出 MessagingException,并且在 MessagingException 的嵌套异常链中包含 UnsupportedEncodingException。

description 内容描述
ThrowsIllegalWriteException: 如果底层实现不支持修改
ThrowsIllegalStateException: 如果此正文部分可从 READ_ONLY 文件夹获取。
ThrowsMessagingException: 其他情况;如果字符集转换失败,则可能在异常链中包含 UnsupportedEncodingException。

英文文档:

setDescription

public void setDescription(String description)
                    throws MessagingException
Set the "Content-Description" header field for this body part. If the description parameter is null, then any existing "Content-Description" fields are removed.

If the description contains non US-ASCII characters, it will be encoded using the platform's default charset. If the description contains only US-ASCII characters, no encoding is done and it is used as is.

Note that if the charset encoding process fails, a MessagingException is thrown, and an UnsupportedEncodingException is included in the chain of nested exceptions within the MessagingException.

Specified by:
setDescription in interface Part
Parameters:
description - content description
Throws:
IllegalWriteException - if the underlying implementation does not support modification
IllegalStateException - if this body part is obtained from a READ_ONLY folder.
MessagingException - otherwise; an UnsupportedEncodingException may be included in the exception chain if the charset conversion fails.

public void setDescription(String description, String charset) throws MessagingException
设置此正文部分的 "Content-Description" 头字段。如果 description 参数为 null,则移除所有现有的 "Content-Description" 字段。

如果 description 包含非 US-ASCII 字符,则它将使用特定的字符集进行编码。如果 description 仅包含 US-ASCII 字符,则不进行编码,它将按原样使用。

注意,如果字符集编码处理失败,则抛出 MessagingException,并且在 MessagingException 的嵌套异常链中包含 UnsupportedEncodingException。

description 描述
charset 编码的字符集
ThrowsIllegalWriteException: 如果底层实现不支持修改
ThrowsIllegalStateException: 如果此正文部分可从 READ_ONLY 文件夹获取。
ThrowsMessagingException: 其他情况;如果字符集转换失败,则可能在异常链中包含 UnsupportedEncodingException。

英文文档:

setDescription

public void setDescription(String description,
                           String charset)
                    throws MessagingException
Set the "Content-Description" header field for this body part. If the description parameter is null, then any existing "Content-Description" fields are removed.

If the description contains non US-ASCII characters, it will be encoded using the specified charset. If the description contains only US-ASCII characters, no encoding is done and it is used as is.

Note that if the charset encoding process fails, a MessagingException is thrown, and an UnsupportedEncodingException is included in the chain of nested exceptions within the MessagingException.

Parameters:
description - Description
charset - Charset for encoding
Throws:
IllegalWriteException - if the underlying implementation does not support modification
IllegalStateException - if this body part is obtained from a READ_ONLY folder.
MessagingException - otherwise; an UnsupportedEncodingException may be included in the exception chain if the charset conversion fails.

public String getFileName() throws MessagingException
获取与此正文部分关联的文件名称。

返回此正文部分的 "Content-Disposition" 头字段的 "filename" 参数的值。如果不可用,则返回此正文部分的 "Content-Type" 头字段的 "name" 参数的值。如果都不存在,则返回 null

如果 mail.mime.encodefilename 系统属性设置为 true,则将使用 MimeUtility.decodeText 方法解码文件名称。尽管 MIME 规范不支持此编码,但许多邮件程序使用此技术支持在文件名称中使用非 ASCII 字符。此属性的默认值为 false。

return 文件名称

英文文档:

getFileName

public String getFileName()
                   throws MessagingException
Get the filename associated with this body part.

Returns the value of the "filename" parameter from the "Content-Disposition" header field of this body part. If its not available, returns the value of the "name" parameter from the "Content-Type" header field of this body part. Returns null if both are absent.

If the mail.mime.encodefilename System property is set to true, the MimeUtility.decodeText method will be used to decode the filename. While such encoding is not supported by the MIME spec, many mailers use this technique to support non-ASCII characters in filenames. The default value of this property is false.

Specified by:
getFileName in interface Part
Returns:
filename
Throws:
MessagingException

public void setFileName(String filename) throws MessagingException
如果可能,设置与此正文部分关联的文件名称。

设置此正文部分的 "Content-Disposition" 头字段的 "filename" 参数。为了与原来的邮件程序兼容,还要设置 "Content-Type" 头的 "name" 参数。

如果 mail.mime.encodefilename 系统属性设置为 true,则将使用 MimeUtility.encodeText 方法编码文件名称。尽管 MIME 规范不支持此编码,但许多邮件程序使用此技术支持在文件名称中使用非 ASCII 字符。此属性的默认值为 false。

ThrowsIllegalWriteException: 如果底层实现不支持修改
ThrowsIllegalStateException: 如果此正文部分可从 READ_ONLY 文件夹获取。

英文文档:

setFileName

public void setFileName(String filename)
                 throws MessagingException
Set the filename associated with this body part, if possible.

Sets the "filename" parameter of the "Content-Disposition" header field of this body part. For compatibility with older mailers, the "name" parameter of the "Content-Type" header is also set.

If the mail.mime.encodefilename System property is set to true, the MimeUtility.encodeText method will be used to encode the filename. While such encoding is not supported by the MIME spec, many mailers use this technique to support non-ASCII characters in filenames. The default value of this property is false.

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

public java.io.InputStream getInputStream() throws java.io.IOException, MessagingException
返回用于此正文部分 "content" 的解码输入流。

此实现从 DataHandler 获取输入流。即它调用 getDataHandler().getInputStream();

return InputStream
ThrowsMessagingException:
Throwsjava.io.IOException: 此异常通常由 DataHandler 抛出。有关更多信息,请参考 javax.activation.DataHandler 的文档。
See also getContentStream, getInputStream

英文文档:

getInputStream

public InputStream getInputStream()
                           throws IOException,
                                  MessagingException
Return a decoded input stream for this body part's "content".

This implementation obtains the input stream from the DataHandler. That is, it invokes getDataHandler().getInputStream();

Specified by:
getInputStream in interface Part
Returns:
an InputStream
Throws:
MessagingException
IOException - this is typically thrown by the DataHandler. Refer to the documentation for javax.activation.DataHandler for more details.
See Also:
getContentStream(), DataHandler.getInputStream()

protected java.io.InputStream getContentStream() throws MessagingException
生成内容的原始字节。在为内容创建 DataHandler 对象时使用此方法。能为 Part 内容提供单独输入流的子类可能要重写此方法。

See also content, getContentStream

英文文档:

getContentStream

protected InputStream getContentStream()
                                throws MessagingException
Produce the raw bytes of the content. This method is used when creating a DataHandler object for the content. Subclasses that can provide a separate input stream for just the Part content might want to override this method.

Throws:
MessagingException
See Also:
content, MimeMessage.getContentStream()

public java.io.InputStream getRawInputStream() throws MessagingException
将 InputStream 作为原始数据返回,保持所有 Content-Transfer-Encoding 不变。如果 "Content-Transfer-Encoding" 头不正确或者被破坏(将导致 getInputStream 方法或 getContent 方法无法返回正确的数据),则此方法很有用。在这种情况下,应用程序可以使用此方法自己尝试解码原始数据。

此实现调用 getContentStream 方法。

since
JavaMail 1.2
See also getInputStream, getContentStream

英文文档:

getRawInputStream

public InputStream getRawInputStream()
                              throws MessagingException
Return an InputStream to the raw data with any Content-Transfer-Encoding intact. This method is useful if the "Content-Transfer-Encoding" header is incorrect or corrupt, which would prevent the getInputStream method or getContent method from returning the correct data. In such a case the application may use this method and attempt to decode the raw data itself.

This implementation simply calls the getContentStream method.

Throws:
MessagingException
Since:
JavaMail 1.2
See Also:
getInputStream(), getContentStream()

public DataHandler getDataHandler() throws MessagingException
返回用于此正文部分的内容的 DataHandler。

在此提供的实现与 MimeMessage 中实现的作用类似。

See also getDataHandler

英文文档:

getDataHandler

public DataHandler getDataHandler()
                           throws MessagingException
Return a DataHandler for this body part's content.

The implementation provided here works just like the the implementation in MimeMessage.

Specified by:
getDataHandler in interface Part
Returns:
DataHandler for the content
Throws:
MessagingException
See Also:
MimeMessage.getDataHandler()

public Object getContent() throws java.io.IOException, MessagingException
以 Java 对象的形式返回内容。返回对象的类型当然取决于内容本身。例如,text/plain 内容的本机格式通常为 String 对象。"multipart" 内容的本机格式通常为 Multipart 子类。对于 DataHandler 系统未知的内容类型,将输入流作为内容返回。

此实现从 DataHandler 获取内容。即它调用 getDataHandler().getContent();如果内容为 Multipart 或 Message 对象,并且是通过解析流创建的,则该对象被缓存并在后续调用中返回,以防止丢失对内容所做的修改。

return 对象
ThrowsMessagingException:
Throwsjava.io.IOException: 此异常通常由 DataHandler 抛出。有关更多信息,请参考 javax.activation.DataHandler 的文档。

英文文档:

getContent

public Object getContent()
                  throws IOException,
                         MessagingException
Return the content as a Java object. The type of the object returned is of course dependent on the content itself. For example, the native format of a text/plain content is usually a String object. The native format 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 implementation obtains the content from the DataHandler. That is, it invokes getDataHandler().getContent(); If the content is a Multipart or Message object and was created by parsing a stream, the object is cached and returned in subsequent calls so that modifications to the content will not be lost.

Specified by:
getContent in interface Part
Returns:
Object
Throws:
MessagingException
IOException - this is typically thrown by the DataHandler. Refer to the documentation for javax.activation.DataHandler for more details.
See Also:
DataHandler.getContent()

public void setDataHandler(DataHandler dh) throws MessagingException
此方法提供设置此正文部分的内容的机制。给定的 DataHandler 对象应该包装实际的内容。
dh 内容的 DataHandler
ThrowsIllegalWriteException: 如果底层实现不支持修改
ThrowsIllegalStateException: 如果此正文部分可从 READ_ONLY 文件夹获取。
英文文档:

setDataHandler

public void setDataHandler(DataHandler dh)
                    throws MessagingException
This method provides the mechanism to set this body part's content. The given DataHandler object should wrap the actual content.

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

public void setContent(Object o, String type) throws MessagingException
设置此正文部分的内容的便捷方法。

内容被包装在 DataHandler 对象中。注意,指定类型的 DataContentHandler 类必须可用于 JavaMail 实现,此方法才能正常运行。也就是说,要实现 setContent(foobar, "application/x-foobar"),就应该安装用于 "application/x-foobar" 的 DataContentHandler。有关更多信息,请参考 Java Activation Framework。

o 内容对象
type 对象的 Mime 类型
ThrowsIllegalWriteException: 如果底层实现不支持修改现有值
ThrowsIllegalStateException: 如果此正文部分可从 READ_ONLY 文件夹获取。

英文文档:

setContent

public void setContent(Object o,
                       String type)
                throws MessagingException
A convenience method for setting this body part's content.

The content is wrapped in a DataHandler object. Note that a DataContentHandler class for the specified type should be available to the JavaMail implementation for this to work right. That is, 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.

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

public void setText(String text) throws MessagingException
使用 MIME 类型 "text/plain" 将给定的 String 设置为此部分内容的便捷方法。如果字符串包含非 US-ASCII 字符,则它将使用平台的默认字符集进行编码。也使用该字符集设置 "charset" 参数。

注意,如果 text 很大,可能会影响性能,因为此方法必须扫描所有字符以确定使用哪种字符集。

如果字符集已知,则使用带有 charset 参数的 setText 方法。

text 要设置的文本内容
ThrowsMessagingException: 如果发生错误
See also setText(String text, String charset)

英文文档:

setText

public void setText(String text)
             throws MessagingException
Convenience method that sets the given String as this part's content, with a MIME type of "text/plain". If the string contains non US-ASCII characters, it will be encoded using the platform's default charset. The charset is also used to set the "charset" parameter.

Note that there may be a performance penalty if text is large, since this method may have to scan all the characters to determine what charset to use.

If the charset is already known, use the setText method that takes the charset parameter.

Specified by:
setText in interface MimePart
Specified by:
setText in interface Part
Parameters:
text - the text content to set
Throws:
MessagingException - if an error occurs
See Also:
setText(String text, String charset)

public void setText(String text, String charset) throws MessagingException
使用 MIME 类型 "text/plain" 和指定字符集将给定 String 设置为此部分内容的便捷方法。给定的 Unicode 字符串将使用指定的字符集进行编码。也使用该字符集设置 "charset" 参数。
text 要设置的文本内容
charset 用于文本的字符集
ThrowsMessagingException: 如果发生错误
英文文档:

setText

public void setText(String text,
                    String charset)
             throws MessagingException
Convenience method that sets the given String as this part's content, with a MIME type of "text/plain" and the specified charset. The given Unicode string will be charset-encoded using the specified charset. The charset is also used to set the "charset" parameter.

Specified by:
setText in interface MimePart
Parameters:
text - the text content to set
charset - the charset to use for the text
Throws:
MessagingException - if an error occurs

public void setText(String text, String charset, String subtype) throws MessagingException
使用基本 MIME 类型 "text" 和指定 MIME 子类型将给定的 String 设置为此部分内容的便捷方法。给定的 Unicode 字符串将使用指定的字符集进行编码。也使用该字符集设置 "charset" 参数。
text 要设置的文本内容
charset 用于文本的字符集
subtype 要使用的 MIME 子类型(如 "html")
ThrowsMessagingException: 如果发生错误
since
JavaMail 1.4
英文文档:

setText

public void setText(String text,
                    String charset,
                    String subtype)
             throws MessagingException
Convenience method that sets the given String as this part's content, with a primary MIME type of "text" and the specified MIME subtype. The given Unicode string will be charset-encoded using the specified charset. The charset is also used to set the "charset" parameter.

Specified by:
setText in interface MimePart
Parameters:
text - the text content to set
charset - the charset to use for the text
subtype - the MIME subtype to use (e.g., "html")
Throws:
MessagingException - if an error occurs
Since:
JavaMail 1.4

public void setContent(Multipart mp) throws MessagingException
此方法将正文部分的内容设置为 Multipart 对象。
mp 为 Message 的内容的 multipart 对象。
ThrowsIllegalWriteException: 如果底层实现不支持修改现有值。
ThrowsIllegalStateException: 如果此正文部分可从 READ_ONLY 文件夹获取。
英文文档:

setContent

public void setContent(Multipart mp)
                throws MessagingException
This method sets the body part's content to a Multipart object.

Specified by:
setContent in interface Part
Parameters:
mp - The multipart object that is the Message's content
Throws:
IllegalWriteException - if the underlying implementation does not support modification of existing values.
IllegalStateException - if this body part is obtained from a READ_ONLY folder.
MessagingException

public void attachFile(java.io.File file) throws java.io.IOException, MessagingException
使用指定的文件为此部分提供数据。file 的名称用作此部分的文件名称,file 中的数据用作此部分的数据。将针对文件数据选择适当的编码。
file 要附加的 File 对象
Throwsjava.io.IOException: 与访问文件有关的错误
ThrowsMessagingException: 与消息有关的错误
since
JavaMail 1.4
英文文档:

attachFile

public void attachFile(File file)
                throws IOException,
                       MessagingException
Use the specified file to provide the data for this part. The simple file name is used as the file name for this part and the data in the file is used as the data for this part. The encoding will be chosen appropriately for the file data.

Parameters:
file - the File object to attach
Throws:
IOException - errors related to accessing the file
MessagingException - message related errors
Since:
JavaMail 1.4

public void attachFile(String file) throws java.io.IOException, MessagingException
使用指定的文件为此部分提供数据。file 的名称用作此部分的文件名称,file 中的数据用作此部分的数据。将针对文件数据选择适当的编码。
file 要附加的文件名
Throwsjava.io.IOException: 与访问文件有关的错误
ThrowsMessagingException: 与消息有关的错误
since
JavaMail 1.4
英文文档:

attachFile

public void attachFile(String file)
                throws IOException,
                       MessagingException
Use the specified file to provide the data for this part. The simple file name is used as the file name for this part and the data in the file is used as the data for this part. The encoding will be chosen appropriately for the file data.

Parameters:
file - the name of the file to attach
Throws:
IOException - errors related to accessing the file
MessagingException - message related errors
Since:
JavaMail 1.4

public void saveFile(java.io.File file) throws java.io.IOException, MessagingException
将此部分的内容保存在指定的文件中。解码内容并保存,不包括任何 MIME 头。
file 要写入的 File 对象
Throwsjava.io.IOException: 与访问文件有关的错误
ThrowsMessagingException: 与消息有关的错误
since
JavaMail 1.4
英文文档:

saveFile

public void saveFile(File file)
              throws IOException,
                     MessagingException
Save the contents of this part in the specified file. The content is decoded and saved, without any of the MIME headers.

Parameters:
file - the File object to write to
Throws:
IOException - errors related to accessing the file
MessagingException - message related errors
Since:
JavaMail 1.4

public void saveFile(String file) throws java.io.IOException, MessagingException
将此部分的内容保存在指定的文件中。解码内容并保存,不包括任何 MIME 头。
file 要写入的文件名
Throwsjava.io.IOException: 与访问文件有关的错误
ThrowsMessagingException: 与消息有关的错误
since
JavaMail 1.4
英文文档:

saveFile

public void saveFile(String file)
              throws IOException,
                     MessagingException
Save the contents of this part in the specified file. The content is decoded and saved, without any of the MIME headers.

Parameters:
file - the name of the file to write to
Throws:
IOException - errors related to accessing the file
MessagingException - message related errors
Since:
JavaMail 1.4

public void writeTo(java.io.OutputStream os) throws java.io.IOException, MessagingException
以 RFC 822 格式流的形式输出正文部分。
ThrowsMessagingException:
Throwsjava.io.IOException: 如果写入流时发生错误,或者 javax.activation 层发生错误。
See also writeTo
英文文档:

writeTo

public void writeTo(OutputStream os)
             throws IOException,
                    MessagingException
Output the body part as an RFC 822 format stream.

Specified by:
writeTo in interface Part
Throws:
MessagingException
IOException - if an error occurs writing to the stream or if an error is generated by the javax.activation layer.
See Also:
DataHandler.writeTo(java.io.OutputStream)

public String[] getHeader(String name) throws MessagingException
获取此 header_name 的所有头。注意,如果某些头包含非 US-ASCII 字符并且应该解码,则可以根据 RFC 2047 进行编码。
name 头名称
return 头数组
See also javax.mail.internet.MimeUtility
英文文档:

getHeader

public String[] getHeader(String name)
                   throws MessagingException
Get all the headers for this header_name. Note that certain headers may be encoded as per RFC 2047 if they contain non US-ASCII characters and these should be decoded.

Specified by:
getHeader in interface Part
Parameters:
name - name of header
Returns:
array of headers
Throws:
MessagingException
See Also:
MimeUtility

public String getHeader(String name, String delimiter) throws MessagingException
获取具有此头名称的所有头,以单个 String 的形式返回,头之间用 delimiter 隔开。如果 delimiter 为 null,则仅返回第一个头。
name 此头的名称
delimiter 返回字符串中头之间的分界符
return 具有此名称的所有头的值字段
ThrowsMessagingException:
英文文档:

getHeader

public String getHeader(String name,
                        String delimiter)
                 throws MessagingException
Get all the headers for this header name, returned as a single String, with headers separated by the delimiter. If the delimiter is null, only the first header is returned.

Specified by:
getHeader in interface MimePart
Parameters:
name - the name of this header
delimiter - delimiter between fields in returned string
Returns:
the value fields for all headers with this name
Throws:
MessagingException

public void setHeader(String name, String value) throws MessagingException
设置此 header_name 的值。使用此新值替换所有现有的头值。注意,RFC 822 头只能包含 US-ASCII 字符,因此包含非 US-ASCII 字符的头必须根据 RFC 2047 的规则进行编码。
name 头名称
value 头值
See also javax.mail.internet.MimeUtility
英文文档:

setHeader

public void setHeader(String name,
                      String value)
               throws MessagingException
Set the value for this header_name. Replaces all existing header values with this new value. Note that RFC 822 headers must contain only US-ASCII characters, so a header that contains non US-ASCII characters must be encoded as per the rules of RFC 2047.

Specified by:
setHeader in interface Part
Parameters:
name - header name
value - header value
Throws:
MessagingException
IllegalWriteException - if the underlying implementation does not support modification of existing values
See Also:
MimeUtility

public void addHeader(String name, String value) throws MessagingException
将此值添加到此 header_name 的现有值中。注意,RFC 822 头只能包含 US-ASCII 字符,因此包含非 US-ASCII 字符的头必须根据 RFC 2047 的规则进行编码。
name 头名称
value 头值
See also javax.mail.internet.MimeUtility
英文文档:

addHeader

public void addHeader(String name,
                      String value)
               throws MessagingException
Add this value to the existing values for this header_name. Note that RFC 822 headers must contain only US-ASCII characters, so a header that contains non US-ASCII characters must be encoded as per the rules of RFC 2047.

Specified by:
addHeader in interface Part
Parameters:
name - header name
value - header value
Throws:
MessagingException
IllegalWriteException - if the underlying implementation does not support modification of existing values
See Also:
MimeUtility

public void removeHeader(String name) throws MessagingException
移除具有此名称的所有头。
英文文档:

removeHeader

public void removeHeader(String name)
                  throws MessagingException
Remove all headers with this name.

Specified by:
removeHeader in interface Part
Parameters:
name - the name of this header
Throws:
MessagingException
IllegalWriteException - if the underlying implementation does not support modification of existing values

public java.util.Enumeration<E> getAllHeaders() throws MessagingException
以 Header 对象的枚举形式返回此 Message 中的所有头。
英文文档:

getAllHeaders

public Enumeration getAllHeaders()
                          throws MessagingException
Return all the headers from this Message as an Enumeration of Header objects.

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

public java.util.Enumeration<E> getMatchingHeaders(String[] names) throws MessagingException
以 Header 对象的枚举形式返回此 Message 中的匹配头。

英文文档:

getMatchingHeaders

public Enumeration getMatchingHeaders(String[] names)
                               throws MessagingException
Return matching headers from this Message as an Enumeration of Header objects.

Specified by:
getMatchingHeaders in interface Part
Returns:
enumeration of Header objects
Throws:
MessagingException

public java.util.Enumeration<E> getNonMatchingHeaders(String[] names) throws MessagingException
以 Header 对象的枚举形式返回此 Message 中不匹配的头。
英文文档:

getNonMatchingHeaders

public Enumeration getNonMatchingHeaders(String[] names)
                                  throws MessagingException
Return non-matching headers from this Message as an Enumeration of Header objects.

Specified by:
getNonMatchingHeaders in interface Part
Returns:
enumeration of Header objects
Throws:
MessagingException

public void addHeaderLine(String line) throws MessagingException
将头行添加到此正文部分
英文文档:

addHeaderLine

public void addHeaderLine(String line)
                   throws MessagingException
Add a header line to this body part

Specified by:
addHeaderLine in interface MimePart
Throws:
IllegalWriteException - if the underlying implementation does not support modification
MessagingException

public java.util.Enumeration<E> getAllHeaderLines() throws MessagingException
以 String 枚举的形式获取所有头行。Header 行是原始 RFC 822 头行,包含 "name" 和 "value" 字段。
英文文档:

getAllHeaderLines

public Enumeration getAllHeaderLines()
                              throws MessagingException
Get all header lines as an Enumeration of Strings. A Header line is a raw RFC 822 header line, containing both the "name" and "value" field.

Specified by:
getAllHeaderLines in interface MimePart
Throws:
MessagingException

public java.util.Enumeration<E> getMatchingHeaderLines(String[] names) throws MessagingException
以 String 枚举的形式获取匹配的头行。Header 行是原始 RFC 822 头行,包含 "name" 和 "value" 字段。
英文文档:

getMatchingHeaderLines

public Enumeration getMatchingHeaderLines(String[] names)
                                   throws MessagingException
Get matching header lines as an Enumeration of Strings. A Header line is a raw RFC 822 header line, containing both the "name" and "value" field.

Specified by:
getMatchingHeaderLines in interface MimePart
Throws:
MessagingException

public java.util.Enumeration<E> getNonMatchingHeaderLines(String[] names) throws MessagingException
以 String 枚举的形式获取不匹配的头行。Header 行是原始 RFC 822 头行,包含 "name" 和 "value" 字段。
英文文档:

getNonMatchingHeaderLines

public Enumeration getNonMatchingHeaderLines(String[] names)
                                      throws MessagingException
Get non-matching header lines as an Enumeration of Strings. A Header line is a raw RFC 822 header line, containing both the "name" and "value" field.

Specified by:
getNonMatchingHeaderLines in interface MimePart
Throws:
MessagingException

protected void updateHeaders() throws MessagingException
检查此正文部分的内容并更新相应的 MIME 头。这里要设置的典型头是 Content-TypeContent-Transfer-Encoding。在两种情况下需要更新头:
- 邮件应用程序创建的消息当然需要在某一时刻激活此方法以填充其内部头。
- 从 Store 读取的消息已经从此存储库中获得了它的所有头,因此不需要此方法。但是,如果此消息可编辑,并且已经对其内容或消息结构进行了某些编辑,则需要重新同步头。
在这两种情况下,此方法通常都由 Message.saveChanges 方法调用。
英文文档:

updateHeaders

protected void updateHeaders()
                      throws MessagingException
Examine the content of this body part and update the appropriate MIME headers. Typical headers that get set here are Content-Type and Content-Transfer-Encoding. Headers might need to be updated in two cases:
- A message being crafted by a mail application will certainly need to activate this method at some point to fill up its internal headers.
- A message read in from a Store will have obtained all its headers from the store, and so doesn't need this. However, if this message is editable and if any edits have been made to either the content or message structure, we might need to resync our headers.
In both cases this method is typically called by the Message.saveChanges method.

Throws:
MessagingException


Submit a bug or feature

Copyright 2007 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

一看就知道只有菜鸟才干这么无知的事啦。

PS : 未经我党受权你也可自由散发此文档。 如有任何错误请自行修正;若因此而造成任何损失请直接找人民主席,请勿与本人联系。谢谢!