⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pop3.java

📁 Java邮箱
💻 JAVA
字号:
package Email.net;

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

public class POP3 implements Serializable{
  protected String server;
  protected int port;

  /** Default POP3 port is 110. */
  public final static int defaultPort = 110;

  protected String user;
  protected String password;

  private String response = "";

  public String getResponse() { return response; }

  protected POP3(){
    server = user = password = "";
    port = defaultPort;
  }

  public POP3(String server, int port, String user, String password){
    this.server = server;
    this.port = port;
    this.user = user;
    this.password = password;
  }

  public POP3(String server, String user, String password){ 
    this(server,defaultPort,user,password); 
    }
  
  //接收邮件
  public Vector readMail( boolean deleteOnServer) throws IOException, UnknownHostException{
    boolean ok;
    int messageCount = 0;
    Vector messages = new Vector();
    Socket s = null;
    BufferedReader in = null;
    PrintWriter out = null;
    try{	
      s = new Socket(server,port);
      in = new BufferedReader( new InputStreamReader( s.getInputStream(),"ISO8859-1" ) );
      out = new PrintWriter( new BufferedOutputStream( s.getOutputStream() ) );
      response = in.readLine();
      ok = response.startsWith( "+OK" );
      if (!ok) 
        throw new IOException( response );
      out.println( "USER " + user );
      out.flush();
      response = in.readLine();
      ok = response.startsWith( "+OK" );
      if (!ok) 
        throw new IOException(response);
      out.println( "PASS " + password );
      out.flush();
      response = in.readLine();
      ok = response.startsWith("+OK");
      if (!ok) 
        throw new IOException(response);
      out.println("STAT");
      out.flush();
      response = in.readLine();
      ok = response.startsWith("+OK");
      if (!ok) 
        throw new IOException( response );
      StringTokenizer st = new StringTokenizer(response," ");
      if ( !st.hasMoreTokens() )
        throw new IOException("STAT error: " + response);
      st.nextToken();
      if ( !st.hasMoreTokens() ) throw new IOException( "STAT error: " + response );
      messageCount = Integer.parseInt( st.nextToken() );       //邮件数量
      for (int i = 1; i <= messageCount; i++)  {
        Email msg = new Email();
        messages.addElement( msg );
        out.println( "RETR "+ i );
        out.flush();
        String buf = in.readLine();
        if ( buf.startsWith("+OK") ) {
          while ( msg.insertHeaderLine( in.readLine() ) );    //邮件头部
          Vector body = new Vector();
          buf = in.readLine();
          while ( !buf.equals(".") ) {
            if ( buf.startsWith(".") )
              body.addElement( buf.substring(1) );
            else
              body.addElement( buf );    
              
            buf = in.readLine();
          }
          if (body.size() > 0)
            msg.attachBody( body ) ;
        }
      }
      
      if (deleteOnServer){
      
        for (int i = 1; i <= messageCount; i++)  {
          out.println("DELE " + i);
          out.flush();
          in.readLine();
          }
          in.close();
       }
        
    } 
    finally  {
      if (in != null)
        in.close();
      if (out != null)
        out.close();
      if (s != null)
        s.close();
    }
    return messages;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -