package coffeecatwebmail;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class prasemimemessage{
private mimemessage mimemessage = null;
private string saveattachpath = ""; //附件下载后的存放目录
private stringbuffer bodytext = new stringbuffer(); //存放邮件内容的stringbuffer对象
private string dateformat = "yy-mm-dd hh:mm"; //默认的日前显示格式
/**
* 构造函数,初始化一个mimemessage对象
*/
public prasemimemessage(){}
public prasemimemessage(mimemessage mimemessage){
this.mimemessage = mimemessage;
system.out.println("create a prasemimemessage object........");
}
public void setmimemessage(mimemessage mimemessage){
this.mimemessage = mimemessage;
}
/**
* 获得发件人的地址和姓名
*/
public string getfrom()throws exception{
internetaddress address[] = (internetaddress[])mimemessage.getfrom();
string from = address[0].getaddress();
if(from == null) from="";
string personal = address[0].getpersonal();
if(personal == null) personal="";
string fromaddr = personal+"<"+from+">";
return fromaddr;
}
/**
* 获得邮件的收件人,抄送,和密送的地址和姓名,根据所传递的参数的不同
* "to"----收件人 "cc"---抄送人地址 "bcc"---密送人地址
*/
public string getmailaddress(string type)throws exception{
string mailaddr = "";
string addtype = type.touppercase();
internetaddress []address = null;
if(addtype.equals("to") || addtype.equals("cc") ||addtype.equals("bcc")){
if(addtype.equals("to")){
address = (internetaddress[])mimemessage.getrecipients(message.recipienttype.to);
}else if(addtype.equals("cc")){
address = (internetaddress[])mimemessage.getrecipients(message.recipienttype.cc);
}else{
address = (internetaddress[])mimemessage.getrecipients(message.recipienttype.bcc);
}
if(address != null){
for(int i=0;i<address.length;i++){
string email=address[i].getaddress();
if(email==null) email="";
else{
email=mimeutility.decodetext(email);
}
string personal=address[i].getpersonal();
if(personal==null) personal="";
else{
personal=mimeutility.decodetext(personal);
}
string compositeto=personal+"<"+email+">";
mailaddr+=","+compositeto;
}
mailaddr=mailaddr.substring(1);
}
}else{
throw new exception("error emailaddr type!");
}
return mailaddr;
}
/**
* 获得邮件主题
*/
public string getsubject()throws messagingexception{
string subject = "";
try{
subject = mimeutility.decodetext(mimemessage.getsubject());
if(subject == null) subject="";
}catch(exception exce){
}
return subject;
}
/**
* 获得邮件发送日期
*/
public string getsentdate()throws exception{
date sentdate = mimemessage.getsentdate();
simpledateformat format = new simpledateformat(dateformat);
return format.format(sentdate);
}
/**
* 获得邮件正文内容
*/
public string getbodytext(){
return bodytext.tostring();
}
/**
* 解析邮件,把得到的邮件内容保存到一个stringbuffer对象中,解析邮件
* 主要是根据mimetype类型的不同执行不同的操作,一步一步的解析
*/
public void getmailcontent(part part)throws exception{
string contenttype = part.getcontenttype();
int nameindex = contenttype.indexof("name");
boolean conname =false;
if(nameindex != -1) conname=true;
system.out.println("contenttype: "+contenttype);
if(part.ismimetype("text/plain") && !conname){
bodytext.append((string)part.getcontent());
}else if(part.ismimetype("text/html") && !conname){
bodytext.append((string)part.getcontent());
}else if(part.ismimetype("multipart/*")){
multipart multipart = (multipart)part.getcontent();
int counts = multipart.getcount();
for(int i=0;i<counts;i++){
getmailcontent(multipart.getbodypart(i));
}
}else if(part.ismimetype("message/rfc822")){
getmailcontent((part)part.getcontent());
}else{}
}
/**
* 判断此邮件是否需要回执,如果需要回执返回"true",否则返回"false"
*/
public boolean getreplysign()throws messagingexception{
boolean replysign = false;
string needreply[] = mimemessage.getheader("disposition-notification-to");
if(needreply != null){
replysign = true;
}
return replysign;
}
/**
* 获得此邮件的message-id
*/
public string getmessageid()throws messagingexception{
return mimemessage.getmessageid();
}
/**
* 【判断此邮件是否已读,如果未读返回返回false,反之返回true】
*/
public boolean isnew()throws messagingexception{
boolean isnew = false;
flags flags = ((message)mimemessage).getflags();
flags.flag []flag = flags.getsystemflags();
system.out.println("flags's length: "+flag.length);
for(int i=0;i<flag.length;i++){
if(flag[i] == flags.flag.seen){
isnew=true;
system.out.println("seen message.......");
break;
}
}
return isnew;
}
/**
* 判断此邮件是否包含附件
*/
public boolean iscontainattach(part part)throws exception{
boolean attachflag = false;
string contenttype = part.getcontenttype();
if(part.ismimetype("multipart/*")){
multipart mp = (multipart)part.getcontent();
for(int i=0;i<mp.getcount();i++){
bodypart mpart = mp.getbodypart(i);
string disposition = mpart.getdisposition();
if((disposition != null) &&((disposition.equals(part.attachment)) ||(disposition.equals(part.inline))))
attachflag = true;
else if(mpart.ismimetype("multipart/*")){
attachflag = iscontainattach((part)mpart);
}else{
string contype = mpart.getcontenttype();
if(conty
闽公网安备 35060202000074号