`
2277259257
  • 浏览: 498597 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

电子邮件系统 2----JavaMail 搜索指定邮件示例

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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics