9 详解Multipart和BodyPart

来源:互联网 发布:游戏鼠标 知乎 编辑:程序博客网 时间:2024/06/05 19:31
A、简介
Message表示一个邮件,messgaes.getContent()返回一个Multipart对象。一个Multipart对象包含一个或多个BodyPart对象,来组成邮件的正文部分(包括附件)。
 
B、Multipart
javax.mail.Multipart
    public abstract class Multipart
Multipart是一个容器它转载多个body Part(正文、附件或内嵌资源)。Part的getContent()方法就返回一个Multipart对象。
javax.mail.internet.MimeMultipart
public class MimeMultipart extends Multipart
MimeMultipart是Multipart的实现类,默认类别是mixed。其他multipart子类型如:related和alternative可以通过new MimeMultipart(“alternative”);来实现。
 
B.0、Multipart的content-type
总体来说,MIME消息由消息头和消息体两大部分组成。现在我们关注的是MIME邮件,因此在以下的讨论中姑且称“消息”为“邮件”。
邮件头包含了发件人、收件人、主题、时间、MIME版本、邮件内容的类型等重要信息。每条信息称为一个域,由域名后加“: ”和信息内容构成,可以是一行,较长的也可以占用多行。域的首行必须“顶头”写,即左边不能有空白字符(空格和制表符);续行则必须以空白字符打头,且第一个空白字符不是信息本身固有的,解码时要过滤掉。
邮件体包含邮件的内容,它的类型由邮件头的“Content-Type”域指出。常见的简单类型有text/plain(纯文本)和text/html(超文本)。有时也会出现的multipart类型,是MIME邮件的精髓。邮件体被分为多个段,每个段又包含段头和段体两部分,这两部分之间也以空行分隔。常见的multipart类型有三种:multipart/mixed, multipart/related和multipart/alternative。
multipart/mixed:附件。
multipart/related:内嵌资源。
multipart/alternative:纯文本与超文本共存。
可以看出,如果在邮件中要添加附件,必须定义multipart/mixed段;如果存在内嵌资源,至少要定义multipart/related段;如果纯文本与超文本共存,至少要定义multipart/alternative段。什么是“至少”?举个例子说,如果只有纯文本与超文本正文,那么在邮件头中将类型扩大化,定义为multipart/related,甚至multipart/mixed,都是允许的。
multipart诸类型的共同特征是,在段头指定“boundary”参数字符串,段体内的每个子段以此串定界。所有的子段都以--=_”boundary行”开始,父段则以--=_”boundary行” --结束。段与段之间也以空行分隔。
前文,在邮件体是multipart类型的情况下,邮件体的开始部分(第一个--=_”boundary行”之前)可以有一些附加的文本行,相当于注释,解码时应忽略。段间也可以有一些附加的文本行,不会显示出来,如果有兴趣,不妨验证一下。
 
B.1、javax.mail.Multipart
属性
protected  String   contentType
This field specifies the content-type of this multipart object.
返回Multipart的content-type类型。类型包括"alternative", "mixed", "related", "parallel", "signed"等。
protected  Part      parent
The Part containing this Multipart, if known.
父Part,一般是message。
protected  Vector  parts
Vector of BodyPart objects.
方法
1.操作BodyPart
void  addBodyPart(BodyPart part)
Adds a Part to the multipart.
void  addBodyPart(BodyPart part, int index)
Adds a BodyPart at position index.
BodyPart getBodyPart(int index)
Get the specified Part.
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).
2.操作父Part
Part getParent()
Return the Part that contains this Multipart object, or null if not known.
void  setParent(Part parent)
Set the parent of this Multipart to be the specified Part.
3.取得content-type
String        getContentType()
Return the content-type of this Multipart.
4.取得BodyPart数量
int    getCount()
Return the number of enclosed BodyPart objects.
 
B.2、javax.mail.internet.MimeMultipart
属性
protected  DataSource ds
The DataSource supplying our InputStream.
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()
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.
创建一个指定子类型的MimeMultipart对象。默认为mixed,你可设置related或alternative等。
方法
1. 操作BodyPart
void  addBodyPart(BodyPart part)
Adds a Part to the multipart.
void  addBodyPart(BodyPart part, int index)
Adds a BodyPart at position index.
BodyPart getBodyPart(int index)
Get the specified BodyPart.
BodyPart getBodyPart(String CID)
Get the MimeBodyPart referred to by the given ContentID (CID).
void  removeBodyPart(int index)
Remove the part at specified location (starting from 0).
2. 操作前文
String        getPreamble()
Get the preamble text, if any, that appears before the first body part of this multipart.
void  setPreamble(String preamble)
Set the preamble text to be included before the first body part.
3.设置子类型
void  setSubType(String subtype)
Set the subtype.
4.
boolean    isComplete()
Return true if the final boundary line for this multipart was seen.
 
C、BodyPart
javax.mail.Part
public interface Part
javax.mail.internet. MimePart
public interface MimePart extends Part
javax.mail.BodyPart
public abstract class BodyPart implements Part
BodyPart是一个包含在Multipart中的Part。它是一个Part也包含attribute和content。
javax.mail.internet.MimeBodyPart
public class MimeBodyPart extends BodyPart implements MimePart
MimeBodyPart是BodyPart的实现类。
javax.mail.internet.PreencodedMimeBodyPart
         public class PreencodedMimeBodyPart extends MimeBodyPart
 
C.1、javax.mail.BodyPart
属性
Multipart parent
The Multipart object containing this BodyPart, if known.
这是和message的区别所在,虽然都实现Part接口,但BodyPart是包含在Multipart中的。它的parent是(Multipart)message.getContent()。
 
C.2、javax.mail.internet.MimeBodyPart
属性
protected  byte[]   content
Byte array that holds the bytes of the content of this Part.
一个字节数组存放BodyPart内容的字节流。
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.
若BodyPart的数据提供一个InputStream,contentStream是另一种流代表BodyPart的内容。
protected  DataHandler        dh
The DataHandler object representing this Part's content.
代表BodyPart内容的DataHandler对象。
protected  InternetHeaders headers
The InternetHeaders object that stores all the headers of this body part.
InternetHeaders 对象存放所有BodyPart的标题。
构造函数
MimeBodyPart()
An empty MimeBodyPart object is created.
创建一个空的MimeBodyPart对象。
MimeBodyPart(InputStream is)
Constructs a MimeBodyPart by reading and parsing the data from the specified input stream.
MimeBodyPart(InternetHeaders headers, byte[] content)
Constructs a MimeBodyPart using the given header and content bytes.
方法
1.操作附件
DataHandler
DataHandler    getDataHandler()
Return a DataHandler for this body part's content.
void  setDataHandler(DataHandler dh)
This method provides the mechanism to set this body part's content.
2.操作附件名
void  setFileName(String filename)
Set the filename associated with this body part, if possible.
设置了附件名称则会在邮件BodyPart头中增加一行表示附件的代码Content-Disposition: attachment; filename=xxxx.xls
String        getFileName()
Get the filename associated with this body part.
获取Content-Disposition: attachment; filename=xxxx.xls中的filename。
3.操作正文
void  setText(String text)
Convenience method that sets the given String as this part's content, with a MIME type of "text/plain".
void  setText(String text, String charset)
Convenience method that sets the given String as this part's content, with a MIME type of "text/plain" and the specified charset.
void  setText(String text, String charset, String subtype)
Convenience method that sets the given String as this part's content, with a primary MIME type of "text" and the specified MIME subtype.
以上三个方法都是用于设置MimeBodyPart的内容(文本内容)的,不同之处在于是否设置指定的字符或指定的MIME类型。
 
 
范例
发送代码
MimeBodyPart mbp_body = ne w MimeBodyPart();
mbp_body.setText(mailMessage.getBody(), "UTF-8");
//正文
MimeBodyPart mbp_attachment=new MimeBodyPart();
DataHandler dh=new DataHandler(new FileDataSource("C:\\xx.xls"));
mbp_attachment.setDataHandler(dh);
mbp_attachment.setFileName("xx.xls");
//附件
MimeMultipart mmp=new MimeMultipart();
mmp.addBodyPart(mbp_body);
mmp.addBodyPart(mbp_attachment);
mimeMessage.setContent(mmp);
接收代码
Object obj = message.getContent();
if (message.isMimeType("text/*")) {                  mailMsg.setBody(obj.toString());
} else if (message.isMimeType("multipart/*")) {
Multipart mp = (Multipart) obj;
    for (int i = 0; i < mp.getCount(); i++) {
Part part = mp.getBodyPart(i);
       if (part.getDisposition() != null
&& part.getDisposition().equals(Part.ATTACHMENT)) {
 
} else if (part.isMimeType("text/*")) {
              part.getContent().toString()
}
    }

}


转自于   http://blog.sina.com.cn/s/blog_6d3c1ec601010bzd.html

0 0