javamail pop 收取邮件

package test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;

public class RecivePOP3mail {
  public static void main(String[] args) throws Exception {

	  //163邮箱
//         String pop3server = "pop3.163.com"; 
//         String user = "******";
//         String pwd = "******";    
//	  	   String port = "110";
  
	  //qq邮箱
//         String pop3server = "pop.qq.com"; 
//         String user = "******@qq.com";
//         String pwd = "******";    
// 	   String port = "110";
       
	  //sina邮箱
//         String pop3server = "pop3.sina.com"; 
//         String user = "******@sina.com";
//         String pwd = "******";     
// 	   String port = "110";
	  
	  //gmail邮箱
      String pop3server = "pop.gmail.com"; // 邮件服务器
      String user = "******";
      String pwd = "******";     // 根据自已的密码修改
      String port = "995";
	  
	  //inspur邮箱
//	    String pop3server = "mail.inspur.com"; // 邮件服务器
//	    String user = "******";
//	    String pwd = "******";     // 根据自已的密码修改
//	    String port = "110";
	  
         Properties props = new Properties();  
         props.setProperty("mail.store.protocol", "pop3");  
         props.setProperty("mail.pop3.host", pop3server);  
         props.setProperty("mail.pop3.port", port); 
         
         //gmail邮箱服务器需要安全链接SSL
         if(pop3server.equals("pop.gmail.com")){
        	 props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
         }
         Session session = Session.getInstance(props);  
         session.setDebug(false); //是否启用debug模式
         Store store = session.getStore("pop3");  
         store.connect(user, pwd);  
         Folder folder = store.getFolder("INBOX");  
         folder.open(Folder.READ_ONLY);  
      
         // 全部邮件数  
         int messageCount = folder.getMessageCount();  
         System.out.println("收件箱总邮件数:"+messageCount);  
       
         //取最新5条打印输出
         int count = 0;
         Message[] messages = folder.getMessages();  
		for (int i = messageCount - 1; i >= 0; i--) {
			if (count == 5) {
				break;
			}
			Message message = messages[i];
			
			// 格式化时间:2013/06/04 18:33:33
			Date sendDate = message.getSentDate();
			if(sendDate != null){
				System.out.println("时间:"+new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(sendDate));
			}
			//邮件主题
			System.out.println("主题:" + message.getSubject());
			
			//解析发件人
			InternetAddress[] address = (InternetAddress[]) message.getFrom();
			InternetAddress addr = address[0];
			String personName = addr.getPersonal();
			if(personName == null){
				personName = "";
			}
			String personEmail = addr.getAddress();
			System.out.println("发件人:"+personName+"<"+ personEmail + ">");
			count++;
			System.out.println("--------------");
		}
  }
}

 
javamail pop 收取邮件
 http://huangqiqing123.iteye.com/blog/1882312

你可能感兴趣的:(javamail)