加入收藏在线咨询
位置:主页 >

尊龙人生就是博d88:基于JavaMail的SearchTerm【自定义的邮件查找器】

作者:dudu时间:11月23日浏览:


上篇文章我写的是关于若何用JavaMail来实现对邮件的查找,在这篇文章中我们继承探究若何用JavaMail来实现邮件查找,这篇文章的重点是若何经由过程扩展JavaMail来实现自定义的邮件查找器

下面我将经由过程两个例子来阐明假如扩展JavaMail的javax.mail.search中的类:

例子一:

经由过程扩展SearchTerm来实现自己的根据附件来查找邮件的JAttachTerm,下面是本法度榜样的

源代码:

//|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

/*

* Created on 2005-1-30

*

* TODO To change the template for this generated file go to

* Window - Preferences - Java - Code Style - Code Templates

*/

package webmail.mail.search;

import javax.mail.Message;

import java尊龙人生就是博d88x.mail.internet.MimeMessage;

import尊龙人生就是博d88 javax.mail.search.StringTerm;

import webmail.mail.尊龙人生就是博d88MimeMessageParser;

/**

*

Title: JSubjectTerm.java尊龙人生就是博d88

*

Description:

*

Copyright: Copyright (c) 2004-2008

*

Company: coffee-mail of BeiJing

*

* @author wang weiping

* @version 1.0 2005-1-30

*/

public class JSubjectTerm extends StringTerm{

public JSubjectTerm(String p尊龙人生就是博d88attern){

super(pattern);

}

/* (non-Javadoc)

* 核心: 主要的便是扩展SearchTerm的match(Message message) 措施

* @see javax.mail.search.SearchTerm#match(javax.mail.Message)

*/

public boolean match(Message message){

String subject = "";

//以下应用自定义的类MimeMessageParser类来实现对MimeMessage的解析

//来得到特定邮件的主题

MimeMessageParser mmp = MimeMessageParser.getMessageParser((MimeMessage)message);

try{

//调用MimeMessageParser类的getSubject()措施来得到邮件的主题

subject = mmp.getSubject();

}catch(Exception ex){

//ignore exception

return false;

}

if(subject == null) return false;

return super.match(subject);

}

//留意必然要覆写StringTerm的equals(Object object)措施,详细来由请参看Effective Java

public boolean equals(Object obj) {

if (! (obj instanceof JSubjectTerm)) {

return false;

}

return super.equals(obj);

}

}

电话:13866999966
联系人:王经理
Q Q:88996699
邮箱:admin@dede58.com
地址:中国XX省XX市XX路XX号