import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.AndTerm;
import javax.mail.search.BodyTerm;
import javax.mail.search.ComparisonTerm;
import javax.mail.search.FromStringTerm;
import javax.mail.search.SearchTerm;
import javax.mail.search.SizeTerm;
import javax.mail.search.SubjectTerm;

/**
 * 怎么使用不同的协议得到不一样的结果,使用IMAP协议居然得到的邮件与使用POP3协议不一样,实现得不一样
 * 那么怎么办呢
 * @author Owner
 */
public class SearchMail {

    public static void main(String[] args) throws Exception{
        Properties env=System.getProperties();
        Session session=Session.getDefaultInstance(env,null);
        Store store=session.getStore("imap");
        session.setDebug(true);
        store.connect("localhost","tx_1982","password");
        Folder folder=store.getFolder("INBOX");
        folder.open(Folder.READ_WRITE);
        
        SearchTerm serch=null;
        
        serch=new SubjectTerm("mySend");
        SearchTerm from=new FromStringTerm("tx_1981@csm.com");
        SearchTerm size=new SizeTerm(ComparisonTerm.GE,1000);
        BodyTerm body=new BodyTerm("e");
        
        AndTerm and=new AndTerm(from,size);
        
        Message[] messages=null;
//      messages=folder.search(serch);
//        messages=folder.search(from);
//        messages=folder.search(size);
//        messages=folder.search(and);
        messages=folder.search(body);
        
        System.out.println("length:"+messages.length);
        for (int i = 0; i < messages.length; i++) {
            Message message=messages[i];
            System.out.println("subject:"+message.getSubject());
        }
    }
}