Java使用Socket读写邮件服务器示例

配置文件:

  1. host=XXX.XX.XX.XX  
  2. port=8888  
  3. max_size=200  
  4. min_size=30  
host=XXX.XX.XX.XX
port=8888
max_size=200
min_size=30

 


  1. package com.wondersgroup.stjt.mail.util;  
  2.   
  3. import java.net.*;  
  4. import java.io.IOException;  
  5. /** 
  6.  
  7. */  
  8. public class SocketAdapter extends  Socket{  
  9.   /** 
  10.    * 连接状态 
  11.  
  12.    */  
  13.   private boolean status=true;  
  14.   /** 
  15.    * 默认的构造函数 
  16.  
  17.    */  
  18.   public SocketAdapter() {  
  19.     super();  
  20.   }  
  21.   public SocketAdapter(String host,int port)throws UnknownHostException,IOException{  
  22.     super(host,port);  
  23.   }  
  24.   /** 
  25.    * 判断此连接是否空闲 
  26.  
  27.    * @return boolean 空闲返回ture,否则false 
  28.    */  
  29.   public boolean isFree(){  
  30.     return status;  
  31.   }  
  32.   /** 
  33.    * 当使用此连接的时候设置状态为false(忙碌) 
  34.    */  
  35.   public void setBusy(){  
  36.     this.status=false;  
  37.   }  
  38.   /** 
  39.    * 当客户端关闭连接的时候状态设置为true(空闲) 
  40.  
  41.    */  
  42.   @Override  
  43.   public void close()throws IOException{  
  44.    // System.out.println("Close : set the status is free ");  
  45.         status=true;  
  46.         super.close();  
  47.   }  
  48.     
  49.   public void destroy() throws IOException{  
  50.     close();  
  51.   
  52.   }  
  53. }  
package com.wondersgroup.stjt.mail.util;

import java.net.*;
import java.io.IOException;
/**
* 
*/
public class SocketAdapter extends  Socket{
  /**
   * 连接状态

   */
  private boolean status=true;
  /**
   * 默认的构造函数

   */
  public SocketAdapter() {
    super();
  }
  public SocketAdapter(String host,int port)throws UnknownHostException,IOException{
    super(host,port);
  }
  /**
   * 判断此连接是否空闲

   * @return boolean 空闲返回ture,否则false
   */
  public boolean isFree(){
    return status;
  }
  /**
   * 当使用此连接的时候设置状态为false(忙碌)
   */
  public void setBusy(){
    this.status=false;
  }
  /**
   * 当客户端关闭连接的时候状态设置为true(空闲)

   */
  @Override
  public void close()throws IOException{
   // System.out.println("Close : set the status is free ");
    	status=true;
		super.close();
  }
  
  public void destroy() throws IOException{
    close();

  }
}


  1. package com.wondersgroup.stjt.mail.util;  
  2.   
  3. import java.net.*;  
  4. import java.util.*;  
  5. import java.io.IOException;   
  6.   
  7. public interface  
  8.     ConnectionProvider {  
  9.   public static final String SERVER_IP ="host";  
  10.   public static final String SERVER_PORT = "port";  
  11.   public static final String MAX_SIZE = "max_size";  
  12.   public static final String MIN_SIZE = "min_size";   
  13.   
  14.   /** 
  15.    *判断连接池内是否有连接 
  16.  
  17.    * @return true 有连接返回true,否则返回false 
  18.    */  
  19.   public boolean isPooled();   
  20.   
  21.   /** 
  22.    * 当此方法被调用的时候提供一个 socket 
  23.    * @see Socket 
  24.    * @return Socket a Connection object. 
  25.    */  
  26.   public SocketAdapter getConnection() throws java.net.SocketException;   
  27.   
  28.   /** 
  29.    * 连接池初始化 
  30.    */  
  31.   public void init() throws UnknownHostException, IOException;   
  32.   
  33.   /** 
  34.    * 连接池重新启动 
  35.  
  36.    */  
  37.   public void restart() throws UnknownHostException, IOException;   
  38.   
  39.   /** 
  40.    * 注销连接池 
  41.  
  42.    */  
  43.   public void destroy() throws  IOException;  
  44. }  
package com.wondersgroup.stjt.mail.util;

import java.net.*;
import java.util.*;
import java.io.IOException; 

public interface
    ConnectionProvider {
  public static final String SERVER_IP ="host";
  public static final String SERVER_PORT = "port";
  public static final String MAX_SIZE = "max_size";
  public static final String MIN_SIZE = "min_size"; 

  /**
   *判断连接池内是否有连接

   * @return true 有连接返回true,否则返回false
   */
  public boolean isPooled(); 

  /**
   * 当此方法被调用的时候提供一个 socket
   * @see Socket
   * @return Socket a Connection object.
   */
  public SocketAdapter getConnection() throws java.net.SocketException; 

  /**
   * 连接池初始化
   */
  public void init() throws UnknownHostException, IOException; 

  /**
   * 连接池重新启动

   */
  public void restart() throws UnknownHostException, IOException; 

  /**
   * 注销连接池

   */
  public void destroy() throws  IOException;
}


  1. package com.wondersgroup.stjt.mail.util;  
  2.   
  3. import java.util.*;  
  4. import java.net.*;  
  5. import java.io.IOException;   
  6.   
  7. /** 
  8. * 
  9. */  
  10. public class MyConnectionProvider  
  11.     implements ConnectionProvider {   
  12.   
  13.   private Properties pro = null;  
  14.   private static ConnectionProvider provider = null;  
  15.   private static Object object_lock = new Object();  
  16.   private static Object[] object_lock_list = new Object[Integer.parseInt(ResourceBundle.getBundle("mailsocket").getString("max_size"))];  
  17.   private String ip;  
  18.   private String port;   
  19.   private static InetSocketAddress inetSockAddr=null;  
  20.   /** 
  21.    * 默认的最大连接数 
  22.    */  
  23.   private int max_size = 20;   
  24.   
  25.   /** 
  26.    * 默认的最小连接数 。。。没用 
  27.  
  28.    */  
  29.   private int min_size = 10;   
  30.   
  31.   /** 
  32.    * Socket connection 
  33.    */  
  34.   private SocketAdapter[] socketpool = null;   
  35.   
  36.   /** 
  37.    * 构造对象的时候初始化连接池 
  38.  
  39.    * @throws UnknownHostException 
  40.    * @throws IOException 
  41.    */  
  42.   private MyConnectionProvider() throws UnknownHostException,  
  43.       IOException {  
  44.       ResourceBundle  res =ResourceBundle.getBundle("mailsocket");  
  45.     ip =res.getString(SERVER_IP);  
  46.     port = res.getString(SERVER_PORT);  
  47.     String max_size_s = res.getString(MAX_SIZE);  
  48.     String min_size_s = res.getString(MIN_SIZE);  
  49.     if (max_size_s != null) {  
  50.       max_size = Integer.parseInt(max_size_s);  
  51.     }  
  52.     if (min_size_s != null) {  
  53.       min_size = Integer.parseInt(min_size_s);  
  54.     }   
  55.   
  56.     init(); //构造对象的时候初始化连接池  
  57.   
  58.   }   
  59.   
  60.   /** 
  61.    * 判断是否已经池化 
  62.    * @return boolean 如果池化返回ture,反之返回false 
  63.    */  
  64.   public boolean isPooled() {  
  65.     if (socketpool != null) {  
  66.       return true;  
  67.     }  
  68.     else return false;  
  69.   }   
  70.   
  71.   /** 
  72.    *返回一个连接 
  73.  
  74.    * @return a Connection object. 
  75.  * @throws IOException  
  76.    */  
  77.   public SocketAdapter getConnection()  {  
  78.       SocketAdapter s = null;  
  79.       //System.out.println("loop");  
  80.     for (int i = 0; i < socketpool.length; i++) {  
  81.         //System.out.println("loop detail i="+i);  
  82.       if (socketpool[i] != null) {  
  83.         //如果有空闲的连接,返回一个空闲连接,如果没有,继续循环  
  84.   
  85.           if(!socketpool[i].isFree()){  
  86.               continue;  
  87.             }else{  
  88.                 synchronized (object_lock_list[i]){  
  89.                  if (socketpool[i].isFree()) {  
  90.                   s = socketpool[i];  
  91.                    
  92.                  // System.out.println(i+" socket isConnected()="+s.isConnected()+" binding"+s.isBound());  
  93.                   try {  
  94.                       s=socketpool[i]=new SocketAdapter(ip, Integer.parseInt(port));  
  95.                     socketpool[i].setBusy();  
  96.                     // System.out.println("socket"+i);  
  97.                   } catch (IOException e) {  
  98.   
  99.                       e.printStackTrace();  
  100.                       return null;  
  101.                   }  
  102.                   return s;  
  103.                  }else{  
  104.                      continue;  
  105.                  }  
  106.                 }  
  107.             }  
  108.            
  109.           
  110.       }  
  111.       else { //如果连接为空,证明超过最小连接数,重新生成连接  
  112.   
  113.         try {  
  114.             System.out.println("new socket"+i);  
  115.           s = socketpool[i] = new SocketAdapter(ip, Integer.parseInt(port));  
  116.           return s;  
  117.         }  
  118.         catch (Exception e) {  
  119.           //never throw  
  120.             return null;  
  121.         }  
  122.       }  
  123.     }  
  124.     return s;  
  125.   }   
  126.   
  127.   /** 
  128.    * 初始化连接池 
  129.    * @throws UnknownHostException  
  130.    * @throws IOException  
  131.    */  
  132.   public void init() throws UnknownHostException, IOException {   
  133.   
  134.     socketpool = new SocketAdapter[max_size];  
  135.   
  136.     for (int i = 0; i < max_size; i++) {  
  137.         object_lock_list[i]=new Object();  
  138.       }  
  139.     
  140.     System.out.println("System init success ....");  
  141.   }   
  142.   
  143.   /** 
  144.    * 重新启动连接池 
  145.  
  146.    * @throws UnknownHostException 
  147.    * @throws IOException 
  148.    */  
  149.   public void restart() throws UnknownHostException, IOException {  
  150.   destroy();  
  151.   init();  
  152.   }   
  153.   
  154.   /** 
  155.    * 注销此连接池 
  156.  * @throws IOException  
  157.    */  
  158.   public void destroy() throws IOException {  
  159.     for (int i = 0; i < socketpool.length; i++) {  
  160.       if (socketpool[i] != null) {  
  161.         SocketAdapter adapter = (SocketAdapter) socketpool[i];  
  162.         adapter.destroy();  
  163.         //System.out.print("" );  
  164.       }  
  165.     }  
  166.     System.out.println("\ndestory success ....");  
  167.   }  
  168.   /** 
  169.    * 生成此连接池实现的对象 
  170.  
  171.    * @throws UnknownHostException 
  172.    * @throws IOException  
  173.    * @return ConnectionProvider  
  174.    */  
  175.   public static ConnectionProvider newInstance() throws  
  176.       UnknownHostException, IOException {  
  177.     if (provider == null) {  
  178.       synchronized (object_lock) {  
  179.         if (provider == null) {  
  180.           provider = new MyConnectionProvider();  
  181.         }  
  182.       }  
  183.     }  
  184.     return provider;  
  185.   }  
  186. /** 
  187.  * 读取properties文件。 
  188.  
  189.  * @param key 
  190.  * @param properName 
  191.  * @return 
  192.  */  
  193.   public static String readProperties(String key, String properName) {  
  194.         ResourceBundle rsrc = null;  
  195.         String value = "";  
  196.         rsrc = ResourceBundle.getBundle(properName);  
  197.         value = rsrc.getString(key);  
  198.         return value;  
  199.   
  200.     }  
  201. }  
package com.wondersgroup.stjt.mail.util;

import java.util.*;
import java.net.*;
import java.io.IOException; 

/**
*
*/
public class MyConnectionProvider
    implements ConnectionProvider { 

  private Properties pro = null;
  private static ConnectionProvider provider = null;
  private static Object object_lock = new Object();
  private static Object[] object_lock_list = new Object[Integer.parseInt(ResourceBundle.getBundle("mailsocket").getString("max_size"))];
  private String ip;
  private String port; 
  private static InetSocketAddress inetSockAddr=null;
  /**
   * 默认的最大连接数
   */
  private int max_size = 20; 

  /**
   * 默认的最小连接数 。。。没用

   */
  private int min_size = 10; 

  /**
   * Socket connection
   */
  private SocketAdapter[] socketpool = null; 

  /**
   * 构造对象的时候初始化连接池

   * @throws UnknownHostException
   * @throws IOException
   */
  private MyConnectionProvider() throws UnknownHostException,
      IOException {
	  ResourceBundle  res =ResourceBundle.getBundle("mailsocket");
    ip =res.getString(SERVER_IP);
    port = res.getString(SERVER_PORT);
    String max_size_s = res.getString(MAX_SIZE);
    String min_size_s = res.getString(MIN_SIZE);
    if (max_size_s != null) {
      max_size = Integer.parseInt(max_size_s);
    }
    if (min_size_s != null) {
      min_size = Integer.parseInt(min_size_s);
    } 

    init(); //构造对象的时候初始化连接池

  } 

  /**
   * 判断是否已经池化
   * @return boolean 如果池化返回ture,反之返回false
   */
  public boolean isPooled() {
    if (socketpool != null) {
      return true;
    }
    else return false;
  } 

  /**
   *返回一个连接

   * @return a Connection object.
 * @throws IOException 
   */
  public SocketAdapter getConnection()  {
	  SocketAdapter s = null;
	  //System.out.println("loop");
    for (int i = 0; i < socketpool.length; i++) {
    	//System.out.println("loop detail i="+i);
      if (socketpool[i] != null) {
        //如果有空闲的连接,返回一个空闲连接,如果没有,继续循环

    	  if(!socketpool[i].isFree()){
    		  continue;
        	}else{
        		synchronized (object_lock_list[i]){
        		 if (socketpool[i].isFree()) {
       			  s = socketpool[i];
       			 
       			 // System.out.println(i+" socket isConnected()="+s.isConnected()+" binding"+s.isBound());
       			  try {
       				  s=socketpool[i]=new SocketAdapter(ip, Integer.parseInt(port));
       				socketpool[i].setBusy();
       				// System.out.println("socket"+i);
       			  } catch (IOException e) {

       				  e.printStackTrace();
       				  return null;
       			  }
       			  return s;
        		 }else{
        			 continue;
        		 }
        		}
        	}
    	 
        
      }
      else { //如果连接为空,证明超过最小连接数,重新生成连接

        try {
        	System.out.println("new socket"+i);
          s = socketpool[i] = new SocketAdapter(ip, Integer.parseInt(port));
          return s;
        }
        catch (Exception e) {
          //never throw
        	return null;
        }
      }
    }
    return s;
  } 

  /**
   * 初始化连接池
   * @throws UnknownHostException 
   * @throws IOException 
   */
  public void init() throws UnknownHostException, IOException { 

    socketpool = new SocketAdapter[max_size];

    for (int i = 0; i < max_size; i++) {
    	object_lock_list[i]=new Object();
      }
  
    System.out.println("System init success ....");
  } 

  /**
   * 重新启动连接池

   * @throws UnknownHostException
   * @throws IOException
   */
  public void restart() throws UnknownHostException, IOException {
  destroy();
  init();
  } 

  /**
   * 注销此连接池
 * @throws IOException 
   */
  public void destroy() throws IOException {
    for (int i = 0; i < socketpool.length; i++) {
      if (socketpool[i] != null) {
        SocketAdapter adapter = (SocketAdapter) socketpool[i];
        adapter.destroy();
        //System.out.print("" );
      }
    }
    System.out.println("\ndestory success ....");
  }
  /**
   * 生成此连接池实现的对象

   * @throws UnknownHostException
   * @throws IOException 
   * @return ConnectionProvider 
   */
  public static ConnectionProvider newInstance() throws
      UnknownHostException, IOException {
    if (provider == null) {
      synchronized (object_lock) {
        if (provider == null) {
          provider = new MyConnectionProvider();
        }
      }
    }
    return provider;
  }
/**
 * 读取properties文件。

 * @param key
 * @param properName
 * @return
 */
  public static String readProperties(String key, String properName) {
		ResourceBundle rsrc = null;
		String value = "";
		rsrc = ResourceBundle.getBundle(properName);
		value = rsrc.getString(key);
		return value;

	}
}


Main

  1. package com.wondersgroup.stjt.mail.action;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7. import java.io.InputStreamReader;  
  8. import java.io.PrintWriter;  
  9. import java.net.InetAddress;  
  10. import java.net.InetSocketAddress;  
  11. import java.net.Socket;  
  12. import java.net.SocketAddress;  
  13. import java.net.UnknownHostException;  
  14. import java.sql.ResultSet;  
  15. import java.sql.SQLException;  
  16. import java.util.Properties;  
  17. import java.util.ResourceBundle;  
  18.   
  19.   
  20. import com.wondersgroup.framework.core.web.action.xwork.AbstractAjaxAction;  
  21. import com.wondersgroup.module.common.ExecuteSql;  
  22. import com.wondersgroup.stjt.util.StringUtil;  
  23. import com.wondersgroup.stjt.mail.util.*;  
  24.   
  25. public class ShmetromailAction extends AbstractAjaxAction {  
  26.     //private String emailDomain = "@shmetro.com";  
  27.     //private String host = "222.66.3.199";  
  28.     //private int port = 8888;  
  29.     private String errMsg = "-";  
  30.     private String okMsg = "+";  
  31.   
  32.     public String getMailDetail() throws UnknownHostException {  
  33.           
  34.         //获得用户名  
  35.   
  36.         String curLoginName = (String) this.getRequest().getSession()  
  37.                 .getAttribute("login_name");  
  38.         String mailLoginName = "";  
  39.         String mailPwd = "";  
  40.   
  41.         //取得用户email地址和email密码!  
  42.   
  43.         ExecuteSql dealsql = new ExecuteSql();  
  44.         String sql = "select t.id,t.email from cs_user t where t.login_name='"+curLoginName+"'";  
  45.         //System.out.println("sql");  
  46.         //dealsql.ExecuteSql(sql);  
  47.         //String mailLoginName="";  
  48.         int iuserid=0;  
  49.         //String mailPwd="";  
  50.         try{  
  51.             ResultSet rs = dealsql.ExecuteDemandSql(sql);  
  52.               
  53.             if(rs.next()){  
  54.                  iuserid = rs.getInt("ID");  
  55.                  mailLoginName=rs.getString("EMAIL");  
  56.                 //System.out.println(iuserid+"----"+mailLoginName);  
  57.             }  
  58.             sql="select t.email_passwd from t_cs_user t where t.id='"+iuserid+"'";  
  59.              rs = dealsql.ExecuteDemandSql(sql);  
  60.              if(rs.next()){  
  61.                   
  62.                  mailPwd=rs.getString("email_passwd");  
  63.                 //System.out.println(iuserid+"--++--"+mailPwd);  
  64.             }  
  65.              rs.close();  
  66.         }catch(Exception e){  
  67.             e.printStackTrace();  
  68.             createJSonData("{\"success\":false, \"results\": \"connectionerr\"}");  
  69.             return AJAX;  
  70.         }finally{  
  71.               
  72.             try {  
  73.                 dealsql.close();  
  74.             } catch (SQLException e) {  
  75.                   
  76.                 e.printStackTrace();  
  77.                 createJSonData("{\"success\":false, \"results\": \"connectionerr\"}");  
  78.                 return AJAX;  
  79.             }  
  80.         }  
  81.           
  82.         //判断地址,密码和发性  
  83.   
  84.         //Socket connection=null;  
  85.         if(StringUtil.isNull(mailLoginName)||!mailLoginName.matches("\\S+@shmetro\\.com")||StringUtil.isNull(mailPwd)){  
  86.             createJSonData("{\"success\":false, \"results\": \"errgetemailifo\"}");  
  87.             return AJAX;  
  88.         }  
  89.         SocketAdapter connection = null;  
  90.         int allMailCount=0;  
  91.         int unReadCount=0;  
  92.         // connect to server  
  93.         try {  
  94.             //获取新的connection   
  95.             ConnectionProvider conpool=MyConnectionProvider.newInstance();  
  96.               
  97.             connection=conpool.getConnection();  
  98.               
  99.             if(connection==null){  
  100.                 createJSonData("{\"success\":false, \"results\": \"errgetconnection\"}");  
  101.                 return AJAX;  
  102.             }  
  103.             //so1.setKeepAlive(true);  
  104.             //connection=so1;  
  105.               
  106.             BufferedReader input = new BufferedReader(new InputStreamReader(  
  107.                     connection.getInputStream()));// 接受  
  108.             PrintWriter out = new PrintWriter(connection.getOutputStream(),  
  109.                     true/* autoFlush */);// 传输  
  110.   
  111.               
  112.             String info = null;// 接受信息  
  113.             // read information from server  
  114.             info = input.readLine();  
  115.               
  116.             //输入邮件地址,密码  
  117.   
  118.             BufferedReader in = new BufferedReader(new InputStreamReader(System.in));  
  119.             String sInput =null;  
  120.             String line = null;  
  121.             out.println("USER " + mailLoginName);  
  122.             System.out.println("USER " + mailLoginName);  
  123.             out.flush();  
  124.             line = input.readLine();  
  125.             System.out.println("line " + line);  
  126.             if(StringUtil.isNull(line)||line.startsWith(errMsg)){  
  127.                 createJSonData("{\"success\":false, \"results\": \"errloginname\"}");  
  128.                 return AJAX;  
  129.             }  
  130.             //System.out.println("1"+line);  
  131.             out.println("PASS " + mailPwd);  
  132.             out.flush();  
  133.             line = input.readLine();  
  134.             //System.out.println("line2 " + line);  
  135.             if(StringUtil.isNull(line)||line.startsWith(errMsg)){  
  136.                 createJSonData("{\"success\":false, \"results\": \"errpwd\"}");  
  137.                 return AJAX;  
  138.             }  
  139.             //System.out.println("2"+line);  
  140.             //进入目录查找邮件信息。  
  141.   
  142.             out.println("CHDIR inbox");  
  143.             line = input.readLine();  
  144.             if(StringUtil.isNull(line)||line.startsWith(errMsg)){  
  145.                 createJSonData("{\"success\":false, \"results\": \"errchdir\"}");  
  146.                 return AJAX;  
  147.             }  
  148.             //System.out.println("3"+line);  
  149.             out.println("LIST");  
  150.             line = input.readLine();  
  151.             if(StringUtil.isNull(line)||line.startsWith(errMsg)){  
  152.                 createJSonData("{\"success\":false, \"results\": \"errchdir\"}");  
  153.                 return AJAX;  
  154.             }  
  155.             int i=0;  
  156.             //获取邮件信息并统计  
  157.   
  158.             while((line = input.readLine()) != null){  
  159.                 //System.out.println(i++);  
  160.                 int temp=getMailInfo(line);  
  161.                 if(temp!=-1){  
  162.                     if(temp!=-2){  
  163.                         allMailCount++;  
  164.                         if(temp==0){  
  165.                             unReadCount++;  
  166.                         }  
  167.                     }else{  
  168.                           
  169.                         break;  
  170.                     }  
  171.                 }else{  
  172.                     createJSonData("{\"success\":false, \"results\": \"errmailinfo\"}");  
  173.                     return AJAX;  
  174.                 }  
  175.                 //System.out.println(line);  
  176.             }  
  177.             //System.out.println(allMailCount+"----"+unReadCount);  
  178.   
  179.         } catch (UnknownHostException e) {  
  180.             // TODO Auto-generated catch block  
  181.             e.printStackTrace();  
  182.   
  183.         } catch (IOException e) {  
  184.             // TODO Auto-generated catch block  
  185.             e.printStackTrace();  
  186.   
  187.         } finally {  
  188.             if (connection != null)  
  189.                 try {  
  190.                     connection.close();  
  191.                 } catch (Exception e) {  
  192.                     // TODO Auto-generated catch block  
  193.                     e.printStackTrace();  
  194.                 }  
  195.         }  
  196.         //返回。  
  197.   
  198.         createJSonData("{\"success\":true, \"results\":{\"allMailCount\": \""+allMailCount+"\", \"unReadCount\": \""+unReadCount+"\"}}");  
  199.         return AJAX;  
  200.     }  
  201.     private static int getMailInfo(String line) {  
  202.         if(line.matches("\\S+\\s+\\S+\\s+\\d+")){  
  203.             String re=line.split("\\s+")[2];  
  204.             return Integer.parseInt(re);  
  205.         }  
  206.         //System.out.println("==="+line+"===");  
  207.         if(line.matches(".")){  
  208.             //System.out.println("yes");  
  209.             return -2;  
  210.         }  
  211.         return -1;  
  212.     }  
  213. public static void main(String[] args) throws IOException{  
  214.     String errMsg = "-";  
  215.     String okMsg = "+";  
  216.     String mailLoginName = "limingmin@shmetro.com";  
  217.     String mailPwd = "1111";  
  218.       
  219.     SocketAdapter connection = null;  
  220.     int allMailCount=0;  
  221.         int unReadCount=0;  
  222.     // connect to server  
  223.     try {  
  224.         //获取新的connection   
  225.         ConnectionProvider conpool=MyConnectionProvider.newInstance();  
  226.           
  227.         connection=conpool.getConnection();  
  228.           
  229.         if(connection==null){  
  230.           
  231.         }  
  232.         //so1.setKeepAlive(true);  
  233.         //connection=so1;  
  234.           
  235.         BufferedReader input = new BufferedReader(new InputStreamReader(  
  236.                 connection.getInputStream()));// 接受  
  237.         PrintWriter out = new PrintWriter(connection.getOutputStream(),  
  238.                 true/* autoFlush */);// 传输  
  239.   
  240.           
  241.         String info = null;// 接受信息  
  242.         // read information from server  
  243.         //info = input.readLine();  
  244.           
  245.         //输入邮件地址,密码  
  246.   
  247.         //BufferedReader in = new BufferedReader(new InputStreamReader(System.in));  
  248.         String sInput =null;  
  249.         String line = null;  
  250.         out.println("USER " + mailLoginName);  
  251.         System.out.println("USER " + mailLoginName);  
  252.         out.flush();  
  253.         line = input.readLine();  
  254.         System.out.println("line " + line);  
  255.         if(StringUtil.isNull(line)||line.startsWith(errMsg)){  
  256.               
  257.         }  
  258.         //System.out.println("1"+line);  
  259.         out.println("PASS " + mailPwd);  
  260.         out.flush();  
  261.         line = input.readLine();  
  262.         //System.out.println("line2 " + line);  
  263.         if(StringUtil.isNull(line)||line.startsWith(errMsg)){  
  264.               
  265.         }  
  266.         //System.out.println("2"+line);  
  267.         //进入目录查找邮件信息。  
  268.   
  269.         out.println("CHDIR inbox");  
  270.         line = input.readLine();  
  271.         if(StringUtil.isNull(line)||line.startsWith(errMsg)){  
  272.               
  273.         }  
  274.         //System.out.println("3"+line);  
  275.         out.println("LIST");  
  276.         line = input.readLine();  
  277.         if(StringUtil.isNull(line)||line.startsWith(errMsg)){  
  278.               
  279.         }  
  280.         int i=0;  
  281.         //获取邮件信息并统计  
  282.   
  283.         while((line = input.readLine()) != null){  
  284.             //System.out.println(i++);  
  285.             int temp=getMailInfo(line);  
  286.             if(temp!=-1){  
  287.                 if(temp!=-2){  
  288.                     allMailCount++;  
  289.                     if(temp==0){  
  290.                         unReadCount++;  
  291.                     }  
  292.                 }else{  
  293.                       
  294.                     break;  
  295.                 }  
  296.             }else{  
  297.               
  298.             }  
  299.             //System.out.println(line);  
  300.         }  
  301.         //System.out.println(allMailCount+"----"+unReadCount);  
  302.   
  303.     } catch (UnknownHostException e) {  
  304.         // TODO Auto-generated catch block  
  305.         e.printStackTrace();  
  306.   
  307.     } catch (IOException e) {  
  308.         // TODO Auto-generated catch block  
  309.         e.printStackTrace();  
  310.         System.out.println("111111111111111111111111111111111111111");  
  311.     } finally {  
  312.         if (connection != null)  
  313.             try {  
  314.                 connection.close();  
  315.             } catch (Exception e) {  
  316.                 // TODO Auto-generated catch block  
  317.                 e.printStackTrace();  
  318.             }  
  319.     }  
  320.   
  321.   }  
  322.   
  323. }  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值