快捷搜索:

用javaMail实现对邮件的查找

用javaMail来实现邮件的查找异常的轻易,我把自己日常平凡的履历总结成如下这篇文章,盼望对大年夜家能有赞助,同时也盼望各位能给

如下法度榜样多提一些自己的见地:

下面是本法度榜样的主要框架:

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

package webmail.mail.search;

import javax.mail.*;

import java.util.Iterator;

import java.util.Date;

import java.util.HashSet;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Properties;

import javax.mail.internet.*;

import javax.mail.search.*;

import webmail.mail.search.PersonalTerm;

import webmail.mail.search.JAttachTerm;

import webmail.mail.search.JBodyTerm;

import webmail.mail.*;

import webmail.mail.MimeMessageParser;

/**

* description: 一个简单的邮件查找类,用来查找特定主题的邮件,特定日期,或者

* 是包孕有附件的邮件

* lastmodify: 2004-10-09

*

注: JavaMail API中的javax.mail.search包中的BodyTerm和AttachTerm在应用时

* 有问题,这里应用我扩展SearchTerm的JBodyTerm和JAttachTerm,同时因为javax.

* mail.search包中的SubjectTerm取邮件主题直接调用JavaMail的getSubject()措施

* 这样获得的结果无意偶尔候不正常,可以扩展SearchTerm来定义自己的Term(这个是下

* 一篇文章要写的内容)

* @author wang weiping of BeiJing flow.Inc

* @version v1.0.1

*/

public class MessageQuerier {

private String body = "";

private ArrayList list = new ArrayList();

private int comparison = ComparisonTerm.EQ;

public static final String OR = "or";

public static final String AND = "and";

//默认构造措施

public MessageQuerier() {}

public void setComparison(String string) {

if (string.equals(">")) {

comparison = ComparisonTerm.GE;

} else if (string.equals(")) {

comparison = ComparisonTerm.LE;

}

}

public int getComparison() {

return this.comparison;

}

/**

* 根据指定的字符串来查找所有邮件主题中包孕此字符串的所有邮件

* @param subject 邮件主题中所要包孕的字符串

*/

public void setSubject(String subject) {

SearchTerm subterm = new JSubjectTerm(subject);

list.add(subterm);

}

public void setRecipients(String recipients){

try{

SearchTerm rterm = new RecipientTerm(Message.RecipientType.TO,new InternetAddress(recipients));

list.add(rterm);

}catch(Exception ex){

//ignore exception

ex.printStackTrace();

}

}

public void setRecipientPersons(String recipientPersons){

SearchTerm rpTerm = new RecipientPersonTerm(recipientPersons);

list.add(rpTerm);

}

/**

* 根据指定的字符串来查找发件人地址中包孕此字符串的所有邮件

* @param from 设置发件人地址中要包孕的字符串

*/

public void setFrom(String from) {

try {

SearchTerm fterm = new FromTerm(new InternetAddress(from));

list.add(fterm);

} catch (Exception ex) {

System.err.println("CoffeeWebMail reportException: " + ex.toString());

ex.printStackTrace();

}

}

/**

* Created 2004-10-9

* 此类用了自定义的查找器PersonalTerm,此类从javax.mail.search.StringTerm

* 中承袭下来,重写了StringTerm类的match()措施

*

根据发件人的姓名来查找此发件人发来的所有邮件

* @param personal 设置发件人的姓名

*/

public void setPersonal(String personal) {

try {

PersonalTerm pterm = new PersonalTerm(personal);

list.add(pterm);

} catch (Exception ex) {

System.err.println("CoffeeWebMail reportException: " + ex.toString());

ex.printStackTrace();

}

}

/**

* Created 2004-10-09

*

* 此类用到了自定义的查找器JAttachTerm,此类从javax.mail.search.SearchTerm中

* 直接承袭下来,重写了SearchTerm类的match()措施

*

根据是否包孕附件来查找匹配的所有邮件

* @param flag 是否有附件的标志 true or false

*/

public void setAttachFlag(boolean flag) {

try {

SearchTerm jaterm = new JAttachTerm(flag);

list.add(jaterm);

} catch (Exception ex) {

System.err.println("CoffeeWebMail reportException: " + ex.toString());

ex.printStackTrace();

}

}

/**

* Created 2004-10-09

*

* 根据给定的字符串来查找邮件正文中包孕此字符串的所有邮件

* @param pattern 邮件正文要包孕的字符串

*/

public void setBodyText(String pattern) {

SearchTerm jbterm = new JBodyTerm(pattern);

list.add(jbterm);

}

// note: The dateFormat iseg: [2004-7-10]

public void setReceiveDate(String date) {

int comparison = getComparison();

Date recdate = null;

try {

recdate = getSearchDate(date);

SearchTerm recterm = new ReceivedDateTerm(comparison, recdate);

list.add(recterm);

} catch (Exception ex) {

System.err.println("CoffeeWebMail reportException: " + ex.toString());

ex.printStackTrace();

}

}

public void setSentDate(String date) {

int comparison = getComparison();

Date sentdate = null;

try {

sentdate = getSearchDate(date);

SearchTerm recterm = new SentDateTerm(comparison, sentdate);

list.add(recterm);

} catch (Exception ex) {

System.err.println("CoffeeWebMail reportException: " + ex.toString());

ex.printStackTrace();

}

}

/**

* 返回所有相符搜索前提的邮件数组

* return all messages that according the searchterm.

* param--srchmode["and" or "or"]

* @param folder Folder要搜索的邮件夹

* @param srchmode String 搜索模式,搜索模式为此中之一: MessageQuerier.OR or MessageQuerier.AND

* @return Message[]相符前提的所有消息数组

*/

public Message[] getSrchMessages(Folder folder, String srchmode) {

SearchTerm sterm = null;

Message[] msgs = null;

//if(list.size() == 0) return null;

SearchTerm[] sterms = new SearchTerm[list.size()];

try {

for (int i = 0; i length; i++) {

sterms[i] = (SearchTerm) list.get(i);

}

if (sterms.length > 1) {

if (srchmode.equals("and")) {

//debug info

//System.out.println("and term :"+sterms.length);

sterm = new AndTerm(sterms);

} else if (srchmode.equals("or")) {

sterm = new OrTerm(sterms);

}

} else if (sterms.length == 1) {

//debug info

//System.out.println("sterm's length is 1");

sterm = sterms[0];

}

if (sterm == null) msgs = folder.getMessages();

else

msgs = folder.search(sterm);

} catch (Exception ex) {

System.err.println("CoffeeWebMail reportException: " + ex.toString());

ex.printStackTrace();

}

return msgs;

}

//帮助措施

private Date getSearchDate(String date) {

String[] ymd = date.split("-");

int year, month, day;

Date srchdate = null;

try {

year = Integer.parseInt(ymd[0]);

month = Integer.parseInt(ymd[1]) - 1;

day = Integer.parseInt(ymd[2]);

Calendar cal = Calendar.getInstance();

cal.set(year, month, day);

srchdate = cal.getTime();

} catch (Exception ex) {

System.err.println("CoffeeWebMail reportException: " + ex.toString());

ex.printStackTrace();

}

return srchdate;

}

//一个测试代码

public static void main(String args[])

throws Exception {

String host = "pop.mail.yahoo.com.cn"; //pop.mail.yahoo.com.cn

String username = "wwp_1124"; //wwp_1124

String password = "3061643"; // passwd

Properties props = new Properties();

Session session = Session.getDefaultInstance(props, null);

Store store = session.getStore("pop3");

store.connect(host, username, password);

Folder folder = store.getFolder("INBOX");

folder.open(Folder.READ_ONLY);

MessageQuerier mq = new MessageQuerier();

//mq.setAttach(true);

//mq.setAttachFlag(true);

mq.setFrom("lcd@gxsw.com");

//mq.setSubject("你好");

//mq.setBodyText("测试");

mq.setPersonal("林昌东");

Message[] msgs = mq.getSrchMessages(folder, MessageQuerier.OR);

System.out.println(msgs.length);

for (int i = 0; i length; i++) {

System.out.println("=====================================>");

MimeMessageParser mmp = MimeMessageParser.getMessageParser( (MimeMessage) msgs[i]);

System.out.println("sentDate: " + mmp.getSentDate());

System.out.println("Subject: " + mmp.getSubject());

System.out.println("from: " + mmp.getAddress(AddressType.FROM));

System.out.println("personal: " + mmp.getPersonal(AddressType.FROM));

//System.out.println("attachFlag: " + mmp.isContainAttach(msgs[i]));

}

}

}

您可能还会对下面的文章感兴趣: