www.pudn.com > mail.rar > CheckMail.java


package email; 
 
 
import java.io.File; 
import java.io.IOException; 
 
 
import java.util.Enumeration; 
import java.util.HashMap; 
 
import java.util.Map; 
import java.util.Properties; 
 
 
 
 
import javax.mail.Authenticator; 
 
import javax.mail.Flags; 
import javax.mail.Folder; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
 
 
import javax.mail.Part; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Store; 
 
import javax.mail.search.FromStringTerm; 
import javax.mail.search.OrTerm; 
import javax.mail.search.SearchTerm; 
import javax.mail.search.SubjectTerm; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 
 
 
 
 
 
public class CheckMail implements CheckMailInf{ 
	private UserConfig userConfig = null; 
	private PerformHelper ph = null ; 
	public CheckMail(){} 
	 
	 private  String SMTP_AUTH_USER="janmyzheung"; 
	 private  String SMTP_AUTH_PWD="19841001"; 
	/** private  Authenticator auth=new Authenticator(){ 
		 public PasswordAuthentication getPasswordAuthentication(){ 
		 return new PasswordAuthentication(SMTP_AUTH_USER,SMTP_AUTH_PWD); 
		 } 
		 };**/ 
     
    private Map map = new HashMap(); 
    //需在Spring BEAN配置文件里填充 
    public void setMap(Map sprMap){ 
    	this.map = sprMap ; 
    } 
    public void setUserConfig(UserConfig userConfig){ 
    	this.userConfig = userConfig ; 
    } 
    public void setPh(PerformHelper ph){ 
    	this.ph = ph ; 
    } 
	public  HttpSession validator(HttpServletRequest request,HttpServletResponse response) throws MessagingException{ 
		 
		    HttpSession httpSession = request.getSession(); 
	 
			 
			final String user_count = request.getParameter("user"); 
		    final String user_pwd = request.getParameter("password"); 
			String pop_host = request.getParameter("host"); 
			String smtp_host = (String)map.get(pop_host); 
			String from = pop_host.replace("pop3.",user_count+"@"); 
			Properties props = new Properties(); 
			props.put("mail.smtp.host",smtp_host); 
			props.put("mail.smtp.auth","true");//认证 
			 
			PopupAuthenticator popupAuthenticator = new PopupAuthenticator(); 
			PasswordAuthentication pop = popupAuthenticator.performCheck(user_count,user_pwd); 
		    Session mailSession=Session.getInstance(props,popupAuthenticator); 
		    //将后继需要使用到的资料存入httpSession 
		    UserConfig userConfig =  
		    	ph.obtainUserConfigInstance(user_count, user_pwd, pop_host, smtp_host, from); 
		   
		    httpSession.setAttribute("mailSession",mailSession) ; 
		    httpSession.setAttribute("userConfig",userConfig) ; 
		    httpSession.setAttribute("user_count",user_count); 
		     
		    mailSession.setDebug(true); 
		    
			Store store=mailSession.getStore("pop3");  
			    
			store.connect(pop_host,user_count,user_pwd); 
		    if(store.isConnected()){ 
		    	store.close(); 
		    	return httpSession ; 
		    }else{ 
		    	return null ; 
		    } 
		    
	}/**** 
	public static void main(String[] args){ 
		String user = "pop3.163.com" ;  
		user = user.replace("pop3.","user@"); 
		System.out.println(user); 
	}****/ 
 
	public String sendMail(HttpServletRequest req,HttpServletResponse res) { 
		res.setContentType("text/html"); 
		return "" ; 
  } 
	public MyInboxCollection obtainMyInboses(UserConfig userConfig) throws Exception{ 
		//this.userConfig = userConfig ; 
		 //获得一个session对象,并设置其属性为true 
		Properties props = new Properties(); 
		props.put("mail.smtp.host",userConfig.getSmtp_host()); 
		props.put("mail.smtp.auth","true");//认证 
		//props.setProperty("mail.pop3.socketFactory.fallback", "false"); 
		PopupAuthenticator popupAuthenticator = new PopupAuthenticator(); 
		PasswordAuthentication pop = popupAuthenticator.performCheck(userConfig.getUser_count(),userConfig.getUser_pwd()); 
	    Session mailSession=Session.getInstance(props,popupAuthenticator); 
		 //輸出調試信息 
	    mailSession.setDebug(true); 
		 //创建一个Store对象,并根据得到的三个参数连接到邮件服务器中的用户邮箱   
		 Store store=mailSession.getStore("pop3"); 
		 MyInboxCollection mc = null; 
		 store.connect(userConfig.getPop_host(),userConfig.getUser_count(),userConfig.getUser_pwd()); 
		 if(store.isConnected()){ 
			 Folder inbox=store.getFolder("INBOX");   
			 inbox.open(Folder.READ_ONLY); 
			 Message msg[]=inbox.getMessages(); 
			 //System.out.println("邮件数:"+inbox.getMessageCount()); 
             //得到邮箱中的e-mail总数 
			 int count=inbox.getMessageCount(); 
			 mc = new MyInboxCollection(); 
			   //设置过滤规则,对接收的e-mail进行过滤, 
		     SearchTerm st=new OrTerm(new SubjectTerm("惊喜"),new FromStringTerm("webmaster@hudax.com")); 
		     Message[] filtermsg=inbox.search(st);   
		     mc.setFilterCount(filtermsg.length); 
		   //对被过滤出的e-mail设置删除标志 
		     for(int i=0;i