java

位置:IT落伍者 >> java >> 浏览文章

用Javamail写的解析MimeMessage的程序


发布日期:2019年06月20日
 
用Javamail写的解析MimeMessage的程序

package coffeecatwebmail;

import javaio*;

import javatext*;

import javautil*;

import javaxmail*;

import javaxmailinternet*;

public class PraseMimeMessage{

private MimeMessage mimeMessage = null;

private String saveAttachPath = ;//附件下载后的存放目录

private StringBuffer bodytext = new StringBuffer(); //存放邮件内容的StringBuffer对象

private String dateformat = yyMMdd HH:mm; //默认的日前显示格式

/**

* 构造函数初始化一个MimeMessage对象

*/

public PraseMimeMessage(){}

public PraseMimeMessage(MimeMessage mimeMessage){

thismimeMessage = mimeMessage;

Systemoutprintln(create a PraseMimeMessage object);

}

public void setMimeMessage(MimeMessage mimeMessage){

thismimeMessage = mimeMessage;

}

/**

* 获得发件人的地址和姓名

*/

public String getFrom()throws Exception{

InternetAddress address[] = (InternetAddress[])mimeMessagegetFrom();

String from = address[]getAddress();

if(from == null) from=;

String personal = address[]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 = typetoUpperCase();

InternetAddress []address = null;

if(addtypeequals(TO) || addtypeequals(CC) ||addtypeequals(BCC)){

if(addtypeequals(TO)){

address = (InternetAddress[])mimeMessagegetRecipients(MessageRecipientTypeTO);

}else if(addtypeequals(CC)){

address = (InternetAddress[])mimeMessagegetRecipients(MessageRecipientTypeCC);

}else{

address = (InternetAddress[])mimeMessagegetRecipients(MessageRecipientTypeBCC);

}

if(address != null){

for(int i=;i<addresslength;i++){

String email=address[i]getAddress();

if(email==null) email=;

else{

email=MimeUtilitydecodeText(email);

}

String personal=address[i]getPersonal();

if(personal==null) personal=;

else{

personal=MimeUtilitydecodeText(personal);

}

String compositeto=personal+<+email+>;

mailaddr+=+compositeto;

}

mailaddr=mailaddrsubstring();

}

}else{

throw new Exception(Error emailaddr type!);

}

return mailaddr;

}

/**

* 获得邮件主题

*/

public String getSubject()throws MessagingException{

String subject = ;

try{

subject = MimeUtilitydecodeText(mimeMessagegetSubject());

if(subject == null) subject=;

}catch(Exception exce){

}

return subject;

}

/**

* 获得邮件发送日期

*/

public String getSentDate()throws Exception{

Date sentdate = mimeMessagegetSentDate();

SimpleDateFormat format = new SimpleDateFormat(dateformat);

return formatformat(sentdate);

}

/**

* 获得邮件正文内容

*/

public String getBodyText(){

return bodytexttoString();

}

/**

* 解析邮件把得到的邮件内容保存到一个StringBuffer对象中解析邮件

* 主要是根据MimeType类型的不同执行不同的操作一步一步的解析

*/

public void getMailContent(Part part)throws Exception{

String contenttype = partgetContentType();

int nameindex = contenttypeindexOf(name);

boolean conname =false;

if(nameindex != ) conname=true;

Systemoutprintln(CONTENTTYPE: +contenttype);

if(partisMimeType(text/plain) && !conname){

bodytextappend((String)partgetContent());

}else if(partisMimeType(text/html) && !conname){

bodytextappend((String)partgetContent());

}else if(partisMimeType(multipart/*)){

Multipart multipart = (Multipart)partgetContent();

int counts = multipartgetCount();

for(int i=;i<counts;i++){

getMailContent(multipartgetBodyPart(i));

}

}else if(partisMimeType(message/rfc)){

getMailContent((Part)partgetContent());

}else{}

}

/**

* 判断此邮件是否需要回执如果需要回执返回true否则返回false

*/

public boolean getReplySign()throws MessagingException{

boolean replysign = false;

String needreply[] = mimeMessagegetHeader(DispositionNotificationTo);

if(needreply != null){

replysign = true;

}

return replysign;

}

/**

* 获得此邮件的MessageID

*/

public String getMessageId()throws MessagingException{

return mimeMessagegetMessageID();

}

/**

* 【判断此邮件是否已读如果未读返回返回false反之返回true】

*/

public boolean isNew()throws MessagingException{

boolean isnew = false;

Flags flags = ((Message)mimeMessage)getFlags();

FlagsFlag []flag = flagsgetSystemFlags();

Systemoutprintln(flagss length: +flaglength);

for(int i=;i<flaglength;i++){

if(flag[i] == FlagsFlagSEEN){

isnew=true;

Systemoutprintln(seen Message);

break;

}

}

return isnew;

}

/**

* 判断此邮件是否包含附件

*/

public boolean isContainAttach(Part part)throws Exception{

boolean attachflag = false;

String contentType = partgetContentType();

if(partisMimeType(multipart/*)){

Multipart mp = (Multipart)partgetContent();

for(int i=;i<mpgetCount();i++){

BodyPart mpart = mpgetBodyPart(i);

String disposition = mpartgetDisposition();

if((disposition != null) &&((dispositionequals(PartATTACHMENT)) ||(dispositionequals(PartINLINE))))

attachflag = true;

else if(mpartisMimeType(multipart/*)){

attachflag = isContainAttach((Part)mpart);

}else{

String contype = mpartgetContentType();

if(conty               

上一篇:Java中文相关技术

下一篇:基于控件构架开发访问JavaMail的控件