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类等)
下面通过实现来说明以上类的用法及含义:
1、搜索发件人为“智联招聘“,而且邮件正文包含“Java工程师“的所有邮件
[java] view plain copy
1. SearchTerm andTerm = new AndTerm(
2. new FromStringTerm("智联招聘"),
3. new BodyTerm("java工程师"));
4. Message[] messages = folder.search(andTerm);
2、搜索发件人为“智联招聘“或主题包含“最新职位信息“的所有邮件
[java] view plain copy
1. SearchTerm orTerm = new OrTerm(
2. new FromStringTerm("智联招聘"),
3. new SubjectTerm("最新职位信息"));
4. Message[] messages = folder.search(orTerm);
3、搜索发件人不包含“智联招聘“所有的邮件
[java] view plain copy
1. SearchTerm notTerm = new NotTerm(new FromStringTerm("智联招聘"));
2. Message[] messages = folder.search(notTerm);
4、搜索周一到今天收到的的所有邮件
[java] view plain copy
1. Calendar calendar = Calendar.getInstance();
2. calendar.set(Calendar.DAY_OF_WEEK, calendar.get(Calendar.DAY_OF_WEEK - (Calendar.DAY_OF_WEEK - 1)) - 1);
3. Date mondayDate = calendar.getTime();
4. SearchTerm comparisonTermGe = new SentDateTerm(ComparisonTerm.GE, mondayDate);
5. SearchTerm comparisonTermLe = new SentDateTerm(ComparisonTerm.LE, new Date());
6. SearchTerm comparisonAndTerm = new AndTerm(comparisonTermGe, comparisonTermLe);
7. Message[] messages = folder.search(comparisonAndTerm);
5、搜索大于或等于100KB的所有邮件
[java] view plain copy
1. int mailSize = 1024 * 100;
2. SearchTerm intComparisonTerm = new SizeTerm(IntegerComparisonTerm.GE, mailSize);
3. Message[] messages = folder.search(intComparisonTerm);
ComparisonTerm类常用于日期和数字比较中,它使用六个常量EQ(=)、GE(>=)、GT(>)、LE(<=)、LT(<)、NE(!=)来表示六种不同的比较操作。
完整代码:
[java] view plain copy
1. package org.yangxin.study.jm;
2.
3. import java.io.BufferedReader;
4. import java.io.InputStreamReader;
5. import java.util.Calendar;
6. import java.util.Date;
7. import java.util.Properties;
8.
9. import javax.mail.Flags.Flag;
10. import javax.mail.Folder;
11. import javax.mail.Message;
12. import javax.mail.Session;
13. import javax.mail.Store;
14. import javax.mail.URLName;
15. import javax.mail.internet.MimeMessage;
16. import javax.mail.internet.MimeUtility;
17. import javax.mail.search.AndTerm;
18. import javax.mail.search.BodyTerm;
19. import javax.mail.search.ComparisonTerm;
20. import javax.mail.search.FromStringTerm;
21. import javax.mail.search.IntegerComparisonTerm;
22. import javax.mail.search.NotTerm;
23. import javax.mail.search.OrTerm;
24. import javax.mail.search.SearchTerm;
25. import javax.mail.search.SentDateTerm;
26. import javax.mail.search.SizeTerm;
27. import javax.mail.search.SubjectTerm;
28.
29. /**
30. * 搜索邮件
31. */
32. public class SearchMailTest {
33.
34. public static void main(String[] args) throws Exception {
35. new Properties();
36. "mail.pop3.auth", "true");
37. Session session = Session.getInstance(props);
38. new URLName("pop3", "pop3.163.com", 110, null, "xyang81@163.com", "yX546900873");
39. Store store = session.getStore(url);
40. store.connect();
41. // 得到收件箱
42. "INBOX");
43. // 以读写模式打开收件箱
44. folder.open(Folder.READ_WRITE);
45.
46. Message[] messages = search(folder);
47.
48. "收件箱中共有:" + folder.getMessageCount() + "封邮件,搜索到" + messages.length + "封符合条件的邮件!");
49.
50. // 解析邮件搜索到的邮件
51. POP3ReceiveMailTest.parseMessage(messages);
52.
53. // 根据用户输入的条件搜索所有邮件,并提示用户是否删除
54. //searchDemo(folder);
55.
56. true);
57. store.close();
58. }
59.
60. public static Message[] search(Folder folder) throws Exception {
61. // 搜索主题包含美食的邮件
62. "java培训";
63. new SubjectTerm(subject);
64.
65. // 搜索发件人包含支付宝的邮件
66. new FromStringTerm("支付宝");
67.
68. // 搜索邮件内容包含"招聘"的邮件
69. new BodyTerm("招聘");
70.
71. // 搜索发件人不包含“智联招聘”的邮件
72. new NotTerm(new FromStringTerm("智联招聘"));
73.
74. // 搜索发件人为“智联招聘”,而且内容包含“Java工程师“的邮件
75. new AndTerm(
76. new FromStringTerm("智联招聘"),
77. new BodyTerm("java工程师"));
78.
79.
80. // 搜索发件人为”智联招聘“或主题包含”最新职位信息“的邮件
81. new OrTerm(
82. new FromStringTerm("智联招聘"),
83. new SubjectTerm("最新职位信息"));
84.
85.
86. // 搜索周一到今天收到的的所有邮件
87. Calendar calendar = Calendar.getInstance();
88. 1)) - 1);
89. Date mondayDate = calendar.getTime();
90. new SentDateTerm(ComparisonTerm.GE, mondayDate);
91. new SentDateTerm(ComparisonTerm.LE, new Date());
92. new AndTerm(comparisonTermGe, comparisonTermLe);
93.
94. // 搜索大于或等100KB的所有邮件
95. int mailSize = 1024 * 100;
96. new SizeTerm(IntegerComparisonTerm.GE, mailSize);
97.
98. return folder.search(intComparisonTerm);
99. }
100. }
实例:根据用户输入的收件人(email地址或姓名)和主题作为搜索条件,并提示用户是否删除搜索到的邮件?
[java] view plain copy
1. /**
2. * 根据用户输入的收件人地址(包括email地址和姓名)和主题作为搜索条件,并提示用户是否删除搜索到的邮件
3. * @param from 收件人
4. * @param subject 主题
5. */
6. public static void searchDemo(Folder folder) throws Exception {
7. "收件箱中一共有" + folder.getMessageCount() + "封邮件。请选择操作:\n";
8. "1、输入收件人\n" + "2、输入主题\n" + "3、开始搜索\n" + "4、退出";
9. System.out.println(notifyMsg);
10. null;
11. null;
12. new BufferedReader(new InputStreamReader(System.in));
13. String oper = reader.readLine().trim();
14. while(!(from != null && subject != null)) {
15. if ("4".equals(oper)) {
16. 0);
17. else {
18. if ("1".equals(oper)) {
19. "请输入收件人:");
20. from = reader.readLine();
21. else if ("2".equals(oper)) {
22. "请输入主题:");
23. subject = reader.readLine();
24. else if ("3".equals(oper)) {
25. if (from == null || subject == null) {
26. "未输入搜索条件,无法进行搜索!");
27. else {
28. break;
29. }
30. }
31. }
32. "请选择操作:");
33. oper = reader.readLine().trim();
34. }
35.
36. "\n系统正在根据搜索条件查询所有邮件,请稍候......\n");
37.
38. // 根据输入的条件,创建SearchTerm实例对象
39. new OrTerm(
40. new FromStringTerm(from),
41. new SubjectTerm(subject)
42. );
43.
44. // 根据搜索条件得到搜索到的邮件列表
45. Message[] messages = folder.search(orTerm);
46.
47. "共搜索到" + messages.length + "封满足搜索条件的邮件!\n\n请选择操作:1、查看邮件\t 2、删除所有邮件");
48.
49. "是否要删除搜索到的邮件?(yes/no)";
50.
51. String searchResultOper = reader.readLine();
52.
53. if ("1".equals(searchResultOper)) {
54. for (Message message : messages) {
55. MimeMessage msg = (MimeMessage) message;
56. String sub = POP3ReceiveMailTest.getSubject(msg);
57. "开始查看第" + msg.getMessageNumber() + "封邮件...");
58. "主题: " + sub);
59. "发件人: " + POP3ReceiveMailTest.getFrom(msg));
60. "收件人:" + POP3ReceiveMailTest.getReceiveAddress(msg, null));
61. "发送时间:" + POP3ReceiveMailTest.getSentDate(msg, null));
62. System.out.println(deleteQuestion);
63. String answer = reader.readLine();
64. if ("yes".equals(answer)) {
65. true);
66. "邮件[" + sub + "]删除成功!");
67. else if ("no".equals(answer)) {
68. "第" + msg.getMessageNumber() + "封邮件查看完成!");
69. else if ("stop".equals(answer)) {
70. 0);
71. }
72. System.out.println();
73. }
74. else {
75. System.out.println(deleteQuestion);
76. String answer = reader.readLine();
77. if ("yes".equals(answer)) {
78. for (Message message : messages) {
79. String sub = MimeUtility.decodeText(message.getSubject());
80. true);
81. "邮件[" + sub + "]删除成功!");
82. }
83. }
84. }
85. }
搜索结果:
1、搜索条件,收件人:智联招聘 主题:最新招聘信息
2、搜索条件,收件人:支付宝 主题:信用卡