package coffeecatwebmail;
import java
import java
import java
import javax
import javax
public class PraseMimeMessage{
private MimeMessage mimeMessage = null;
private String saveAttachPath =
private StringBuffer bodytext = new StringBuffer(); //存放郵件內容的StringBuffer對象
private String dateformat =
/**
* 構造函數
*/
public PraseMimeMessage(){}
public PraseMimeMessage(MimeMessage mimeMessage){
this
System
}
public void setMimeMessage(MimeMessage mimeMessage){
this
}
/**
* 獲得發件人的地址和姓名
*/
public String getFrom()throws Exception{
InternetAddress address[] = (InternetAddress[])mimeMessage
String from = address[
if(from == null) from=
String personal = address[
if(personal == null) personal=
String fromaddr = personal+
return fromaddr;
}
/**
* 獲得郵件的收件人
*
*/
public String getMailAddress(String type)throws Exception{
String mailaddr =
String addtype = type
InternetAddress []address = null;
if(addtype
if(addtype
address = (InternetAddress[])mimeMessage
}else if(addtype
address = (InternetAddress[])mimeMessage
}else{
address = (InternetAddress[])mimeMessage
}
if(address != null){
for(int i=
String email=address[i]
if(email==null) email=
else{
email=MimeUtility
}
String personal=address[i]
if(personal==null) personal=
else{
personal=MimeUtility
}
String compositeto=personal+
mailaddr+=
}
mailaddr=mailaddr
}
}else{
throw new Exception(
}
return mailaddr;
}
/**
* 獲得郵件主題
*/
public String getSubject()throws MessagingException{
String subject =
try{
subject = MimeUtility
if(subject == null) subject=
}catch(Exception exce){
}
return subject;
}
/**
* 獲得郵件發送日期
*/
public String getSentDate()throws Exception{
Date sentdate = mimeMessage
SimpleDateFormat format = new SimpleDateFormat(dateformat);
return format
}
/**
* 獲得郵件正文內容
*/
public String getBodyText(){
return bodytext
}
/**
* 解析郵件
* 主要是根據MimeType類型的不同執行不同的操作
*/
public void getMailContent(Part part)throws Exception{
String contenttype = part
int nameindex = contenttype
boolean conname =false;
if(nameindex !=
System
if(part
bodytext
}else if(part
bodytext
}else if(part
Multipart multipart = (Multipart)part
int counts = multipart
for(int i=
getMailContent(multipart
}
}else if(part
getMailContent((Part)part
}else{}
}
/**
* 判斷此郵件是否需要回執
*/
public boolean getReplySign()throws MessagingException{
boolean replysign = false;
String needreply[] = mimeMessage
if(needreply != null){
replysign = true;
}
return replysign;
}
/**
* 獲得此郵件的Message
*/
public String getMessageId()throws MessagingException{
return mimeMessage
}
/**
* 【判斷此郵件是否已讀
*/
public boolean isNew()throws MessagingException{
boolean isnew = false;
Flags flags = ((Message)mimeMessage)
Flags
System
for(int i=
if(flag[i] == Flags
isnew=true;
System
break;
}
}
return isnew;
}
/**
* 判斷此郵件是否包含附件
*/
public boolean isContainAttach(Part part)throws Exception{
boolean attachflag = false;
String contentType = part
if(part
Multipart mp = (Multipart)part
for(int i=
BodyPart mpart = mp
String disposition = mpart
if((disposition != null) &&((disposition
attachflag = true;
else if(mpart
attachflag = isContainAttach((Part)mpart);
}else{
String contype = mpart
if(conty
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19435.html