俊星的BLOG

JAVA MAIL之搜索邮件

1、主要代码:

    /**
     * 搜索邮件
     
*/

    
public static void search(String subject, String from, boolean or) throws Exception {
        Session session 
= Session.getDefaultInstance(System.getProperties(), null);
        
// session.setDebug(true);
        Store store = session.getStore(new URLName("imap://test:test@127.0.0.1"));
        store.connect();
        Folder folder 
= store.getDefaultFolder();
        
// 在收件箱中搜索
        folder = folder.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);
        List
<SearchTerm> terms = new ArrayList<SearchTerm>();
        
// 按主题查询
        terms.add(new SubjectTerm(subject));
        
// 按发件人查询
        terms.add(new FromStringTerm(from));
        
// 一个小时内的邮件(我本地的Megic Winmail邮件服务器查不到内容)
        
// long time = System.currentTimeMillis();
        
// SentDateTerm dateTerm = new SentDateTerm(ComparisonTerm.GE, new Date(
        
// time - 60 * 60 * 1000));
        
// terms.add(dateTerm);
        SearchTerm arrays[] = new SearchTerm[terms.size()];
        terms.toArray(arrays);
        SearchTerm term 
= or ? new OrTerm(arrays) : new AndTerm(arrays);
        Message[] msgs 
= folder.search(term);
        System.out.println(
"FOUND " + msgs.length + " MESSAGES");
        
for (int i = 0; i < msgs.length; i++{
            System.out.println(
"--------------------------");
            System.out.println(
"MESSAGE #" + (i + 1+ ":");
            dumpEnvelope(msgs[i]);
        }

    }


    
/**
     * 打印邮件的内容
     * 
     * 
@param m
     * 
@throws Exception
     
*/

    
public static void dumpEnvelope(Message m) throws Exception {
        Address[] a;
        
if ((a = m.getFrom()) != null{
            
for (int j = 0; j < a.length; j++)
                System.out.println(
"FROM: " + a[j].toString());
        }

        
if ((a = m.getRecipients(Message.RecipientType.TO)) != null{
            
for (int j = 0; j < a.length; j++{
                System.out.println(
"TO: " + a[j].toString());
            }

        }

        System.out.println(
"SUBJECT: " + m.getSubject());
        Date d 
= m.getSentDate();
        System.out.println(
"SendDate: " + (d != null ? d.toString() : "UNKNOWN"));
    }


    
public static void main(String[] args) {
        
try {
            search(
"subject""test2@test.com"false);
            System.out.println(
"\n");
            search(
"Fw: test""test2@test.com"false);
            System.out.println(
"\n");
            search(
"null""test2@test.com"true);
        }
 catch (Exception e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

2、测试输出:
FOUND 0 MESSAGES


FOUND 
1 MESSAGES
--------------------------
MESSAGE #
1:
FROM: test2 
<test2@test.com>
TO: test 
<test@test.com>
SUBJECT: Fw: test
SendDate: Tue Apr 
21 20:38:23 CST 2009


FOUND 
2 MESSAGES
--------------------------
MESSAGE #
1:
FROM: test2 
<test2@test.com>
TO: test 
<test@test.com>
SUBJECT: 测试邮件
SendDate: Mon Apr 
20 21:42:53 CST 2009
--------------------------
MESSAGE #
2:
FROM: test2 
<test2@test.com>
TO: test 
<test@test.com>
SUBJECT: Fw: test
SendDate: Tue Apr 
21 20:38:23 CST 2009

3、相关说明:
如果采用debug模式的话,可以看到调用和搜索串之间的对应关系:
第一次:SEARCH SUBJECT subject FROM test2@test.com ALL
第二次:SEARCH SUBJECT "Fw: test" FROM test2@test.com ALL
第三次:SEARCH OR SUBJECT null FROM test2@test.com ALL

posted on 2009-04-21 20:33 俊星 阅读(244) 评论(0)  编辑  收藏 所属分类: 代码库


只有注册用户登录后才能发表评论。


网站导航: