1. /**
2. * CrazyItTest
3. * 使用JavaMail 搜索并删除指定邮件示例
4. */
5. package
6. import
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15. import
16. import
17. import
18. import
19. import
20. /**
21. * @author Bill Tu(tujiyue/iwtxokhtd)
22. * May 27, 2011[10:04:20 PM]
23. *
24. */
25. public class
26. private static final String PROTOCOL = "pop3";
27. private static final String HOST = "pop3.163.com";
28. private static final String USER = "iwtxokhtd";
29. private static final String PASS="123456";
30.
31. private static
32. new
33. "mail.store.protocol", PROTOCOL);
34. "mail.pop3.host", HOST);
35.
36. Session session=Session.getDefaultInstance(props);
37. return
38. }
39.
40.
41. private static void searchEmail() throws
42. Store store=getMailSession().getStore();
43. store.connect(HOST,USER,PASS);
44.
45. "inbox");//对于POP3协议此参数值只能是inbox
46. //设置收件夹打开后的读写权限
47.
48. int
49. "总共拥有邮件数:"+messageCount);
50.
51. if(messageCount>0){
52.
53. //搜索指定条件的邮件,还有许多其它的搜索条件,请查阅相关的API
54. new AndTerm(new FromStringTerm("[email protected]"),
55. new SubjectTerm("订单8480917155已收到,正在处理中"));
56.
57. Message []messages=receiveFolder.search(search);
58. int
59. if(count<=0){
60. "对不起,没搜到你要的邮件");
61. else{
62. "共搜索到"+count+"封符合条件的邮件!");
63. for(Message msg:messages){
64. "发件人:"+msg.getFrom()[0]+",主题为:"+msg.getSubject());
65. "您是否要删除此邮件?若要请输入yes:");
66. new BufferedReader(new
67. String input=br.readLine();
68. if(input!=null && !"".equals(input)
69. "yes".equals(input.trim())){
70. //执行后面的关闭才会真正删除掉此邮件
71. true);
72. }
73. }
74. }
75. }
76. true);//关闭邮件夹对象
77. //断开连接
78.
79.
80. }
81.
82.
83.
84. /**
85. * @param args
86. * @throws IOException
87. * @throws MessagingException
88. */
89. public static void main(String[] args) throws
90. searchEmail();
91. }
92. }
/** * CrazyItTest * 使用JavaMail 搜索并删除指定邮件示例 */ package com.labci.javamail.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Properties; import javax.mail.Flags; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Store; import javax.mail.search.AndTerm; import javax.mail.search.FromStringTerm; import javax.mail.search.SearchTerm; import javax.mail.search.SubjectTerm; /** * @author Bill Tu(tujiyue/iwtxokhtd) * May 27, 2011[10:04:20 PM] * */ public class SearchEmailTest { private static final String PROTOCOL = "pop3"; private static final String HOST = "pop3.163.com"; private static final String USER = "iwtxokhtd"; private static final String PASS="123456"; private static Session getMailSession(){ Properties props=new Properties(); props.put("mail.store.protocol", PROTOCOL); props.put("mail.pop3.host", HOST); Session session=Session.getDefaultInstance(props); return session; } private static void searchEmail() throws MessagingException, IOException{ Store store=getMailSession().getStore(); store.connect(HOST,USER,PASS); Folder receiveFolder=store.getFolder("inbox");//对于POP3协议此参数值只能是inbox receiveFolder.open(Folder.READ_WRITE);//设置收件夹打开后的读写权限 int messageCount=receiveFolder.getMessageCount(); System.out.println("总共拥有邮件数:"+messageCount); if(messageCount>0){ //搜索指定条件的邮件,还有许多其它的搜索条件,请查阅相关的API SearchTerm search=new AndTerm(new FromStringTerm("[email protected]"), new SubjectTerm("订单8480917155已收到,正在处理中")); Message []messages=receiveFolder.search(search); int count=messages.length; if(count<=0){ System.out.println("对不起,没搜到你要的邮件"); }else{ System.out.println("共搜索到"+count+"封符合条件的邮件!"); for(Message msg:messages){ System.out.println("发件人:"+msg.getFrom()[0]+",主题为:"+msg.getSubject()); System.out.println("您是否要删除此邮件?若要请输入yes:"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String input=br.readLine(); if(input!=null && !"".equals(input) &&"yes".equals(input.trim())){ //执行后面的关闭才会真正删除掉此邮件 msg.setFlag(Flags.Flag.DELETED, true); } } } } receiveFolder.close(true);//关闭邮件夹对象 store.close();//断开连接 } /** * @param args * @throws IOException * @throws MessagingException */ public static void main(String[] args) throws MessagingException, IOException { searchEmail(); } }
标签:search,String,示例,电子邮件系统,private,----,static,new,import
From: https://blog.51cto.com/iwtxokhtd/6248013