JavaMail学习笔记搜索邮件
Posted 幸福过饱和
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaMail学习笔记搜索邮件相关的知识,希望对你有一定的参考价值。
JavaMail在javax.mail.search包中定义了一个用于创建搜索条件的SearchTerm类,应用程序创建SearchTerm类的实例对象后,就可以调用Folder.Search(SearchTerm st)方法搜索邮件夹中符合搜索条件的所有邮件。SearchTerm是一个抽象类,JavaMail提供了22个实现子类以帮助应用程序创建不同的搜索条件,这22个类可分为两大类型,如下所示:
1、用于创建逻辑组合关系的类
- AND条件(AndTerm类)
- OR条件(OrTerm类)
- NOT条件(NotTerm类)
- Comparison条件(ComparisonTerm类)
2、用于创建具体搜索条件的类
- DATE条件(SentDateTerm、ReceivedDateTerm类)
- CONTENT条件(BodyTerm类)
- HEADER条件(FromStringTerm、RecipientStringTerm、SubjectTerm类等)
- SearchTerm andTerm = new AndTerm(
- new FromStringTerm("智联招聘"),
- new BodyTerm("java工程师"));
- Message[] messages = folder.search(andTerm);
[java] view plain copy
- SearchTerm orTerm = new OrTerm(
- new FromStringTerm("智联招聘"),
- new SubjectTerm("最新职位信息"));
- Message[] messages = folder.search(orTerm);
[java] view plain copy
- SearchTerm notTerm = new NotTerm(new FromStringTerm("智联招聘"));
- Message[] messages = folder.search(notTerm);
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.DAY_OF_WEEK, calendar.get(Calendar.DAY_OF_WEEK - (Calendar.DAY_OF_WEEK - 1)) - 1);
- Date mondayDate = calendar.getTime();
- SearchTerm comparisonTermGe = new SentDateTerm(ComparisonTerm.GE, mondayDate);
- SearchTerm comparisonTermLe = new SentDateTerm(ComparisonTerm.LE, new Date());
- SearchTerm comparisonAndTerm = new AndTerm(comparisonTermGe, comparisonTermLe);
- Message[] messages = folder.search(comparisonAndTerm);
- int mailSize = 1024 * 100;
- SearchTerm intComparisonTerm = new SizeTerm(IntegerComparisonTerm.GE, mailSize);
- Message[] messages = folder.search(intComparisonTerm);
完整代码: [java] view plain copy
- package org.yangxin.study.jm;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Properties;
- import javax.mail.Flags.Flag;
- import javax.mail.Folder;
- import javax.mail.Message;
- import javax.mail.Session;
- import javax.mail.Store;
- import javax.mail.URLName;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeUtility;
- import javax.mail.search.AndTerm;
- import javax.mail.search.BodyTerm;
- import javax.mail.search.ComparisonTerm;
- import javax.mail.search.FromStringTerm;
- import javax.mail.search.IntegerComparisonTerm;
- import javax.mail.search.NotTerm;
- import javax.mail.search.OrTerm;
- import javax.mail.search.SearchTerm;
- import javax.mail.search.SentDateTerm;
- import javax.mail.search.SizeTerm;
- import javax.mail.search.SubjectTerm;
- /**
- * 搜索邮件
- */
- public class SearchMailTest
- public static void main(String[] args) throws Exception
- Properties props = new Properties();
- props.setProperty("mail.pop3.auth", "true");
- Session session = Session.getInstance(props);
- URLName url = new URLName("pop3", "pop3.163.com", 110, null, "xyang81@163.com", "yX546900873");
- Store store = session.getStore(url);
- store.connect();
- // 得到收件箱
- Folder folder = store.getFolder("INBOX");
- // 以读写模式打开收件箱
- folder.open(Folder.READ_WRITE);
- Message[] messages = search(folder);
- System.out.println("收件箱中共有:" + folder.getMessageCount() + "封邮件,搜索到" + messages.length + "封符合条件的邮件!");
- // 解析邮件搜索到的邮件
- POP3ReceiveMailTest.parseMessage(messages);
- // 根据用户输入的条件搜索所有邮件,并提示用户是否删除
- //searchDemo(folder);
- folder.close(true);
- store.close();
- public static Message[] search(Folder folder) throws Exception
- // 搜索主题包含美食的邮件
- String subject = "java培训";
- SearchTerm subjectTerm = new SubjectTerm(subject);
- // 搜索发件人包含支付宝的邮件
- SearchTerm fromTerm = new FromStringTerm("支付宝");
- // 搜索邮件内容包含"招聘"的邮件
- SearchTerm bodyTerm = new BodyTerm("招聘");
- // 搜索发件人不包含“智联招聘”的邮件
- SearchTerm notTerm = new NotTerm(new FromStringTerm("智联招聘"));
- // 搜索发件人为“智联招聘”,而且内容包含“Java工程师“的邮件
- SearchTerm andTerm = new AndTerm(
- new FromStringTerm("智联招聘"),
- new BodyTerm("java工程师"));
- // 搜索发件人为”智联招聘“或主题包含”最新职位信息“的邮件
- SearchTerm orTerm = new OrTerm(
- new FromStringTerm("智联招聘"),
- new SubjectTerm("最新职位信息"));
- // 搜索周一到今天收到的的所有邮件
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.DAY_OF_WEEK, calendar.get(Calendar.DAY_OF_WEEK - (Calendar.DAY_OF_WEEK - 1)) - 1);
- Date mondayDate = calendar.getTime();
- SearchTerm comparisonTermGe = new SentDateTerm(ComparisonTerm.GE, mondayDate);
- SearchTerm comparisonTermLe = new SentDateTerm(ComparisonTerm.LE, new Date());
- SearchTerm comparisonAndTerm = new AndTerm(comparisonTermGe, comparisonTermLe);
- // 搜索大于或等100KB的所有邮件
- int mailSize = 1024 * 100;
- SearchTerm intComparisonTerm = new SizeTerm(IntegerComparisonTerm.GE, mailSize);
- return folder.search(intComparisonTerm);
- /**
- * 根据用户输入的收件人地址(包括email地址和姓名)和主题作为搜索条件,并提示用户是否删除搜索到的邮件
- * @param from 收件人
- * @param subject 主题
- */
- public static void searchDemo(Folder folder) throws Exception
- String notifyMsg = "收件箱中一共有" + folder.getMessageCount() + "封邮件。请选择操作:\\n";
- notifyMsg += "1、输入收件人\\n" + "2、输入主题\\n" + "3、开始搜索\\n" + "4、退出";
- JavaMail学习笔记帐号激活与忘记密码 实例(zhuan)