pop3connection.java~1~
来自「实例大全」· JAVA~1~ 代码 · 共 724 行 · 第 1/2 页
JAVA~1~
724 行
package maildemo;import java.io.*;import java.net.*;import java.security.*;import java.util.*;public class Pop3Connection { private Socket socket; private BufferedReader in; private PrintWriter out; public static final int DEFAULT_PORT = 110; private int state; private static final int NONE = 0; private static final int AUTHORIZATION = 1; private static final int TRANSACTION = 2; private static final int UPDATE = 3; private boolean connected; private String response; private String nonce; private int authentication = AUTO; /** * Constant passed to <CODE>setAuthentication</CODE>. When * authentication is set to AUTO, the <CODE>authenticate</CODE> * method will first attempt to authenticate the user using the * APOP command only if a timestamp was presented when the * connection was opened. If not, or if the APOP fails for * some reason, the USER/PASS commands are used. * * @see #setAuthentication * @see #authenticate */ public static final int AUTO = 0; /** * Constant passed to <CODE>setAuthentication</CODE>. This is the * default value. * * @see #setAuthentication * @see #authenticate */ public static final int USER = 1; /** * Constant passed to <CODE>setAuthentication</CODE>. Set it to this * value if you want to use the APOP command for authenticating the * user's identity. Without sending their password in the clear. * Note that most servers don't support this option. * * @see #setAuthentication * @see #authenticate */ public static final int APOP = 2; private int totalMessages = -1; private int totalOctets = -1; private int octets; private Vector octetsVector; private String uid; private Vector uidVector; private boolean debug; private PrintWriter debugOut; public Pop3Connection() { } private void sendCommand(String s) throws IOException { out.print(s + "\r\n"); out.flush(); if (debug) { debugOut.println("C: " + s); } } private String getLine() throws IOException { response = in.readLine(); if (debug) { debugOut.println("S: " + response); } return response; } private boolean getResponse() throws IOException { response = in.readLine(); if (debug) { debugOut.println("S: " + response); } return (response.startsWith("+OK")); } /** * Sets the authentication mechanism to use for this connection. * Defaults to USER. * * @param auth the authentication mechanism to use * @see #USER * @see #APOP * @see #getAuthentication */ public void setAuthentication(int auth) { authentication = auth; } /** * Gets the authentication mechanism being used for this connection. * * @return the currently selected authentication mechanism * @see #USER * @see #APOP * @see #setAuthentication */ public int getAuthentication() { return authentication; } /** * Open a connection to the specified host and port. * * @param host the domain name of the POP3 server * @param port the port to connect to on the host * @return true if connection successful, false if failed * @exception UnknownHostException if the host could not be found * @exception IOException if an input or output exception occurred * @see #open(String) */ public boolean open(String host, int port) throws UnknownHostException, IOException { socket = new Socket(host, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); connected = true; nonce = null; if (getResponse()) { int i = response.indexOf("<"); if (i != -1) { nonce = response.substring(i, response.indexOf(">") + 1); if (debug) { System.out.println("nonce: " + nonce); } } return true; } return false; } /** * Open a connection to the specified host using the default port. * * @param host the domain name of the POP3 server * @return true if connection successful, false if failed * @exception UnknownHostException if the host could not be found * @exception IOException if an input or output exception occurred * @see #open(String, int) */ public boolean open(String host) throws UnknownHostException, IOException { return open(host, DEFAULT_PORT); } /** * Obtain authorization to access a mailbox. The connection must have * already been opened using the <CODE>open</CODE> method. The * authentication mechanism (USER/PASS or APOP) is set using the * <CODE>setAuthentication</CODE> method. * * @param user the name of the account * @param pass the account's password * @return true if authorization was granted, false if not * @exception IOException if an input or output exception occurred * @see #open * @see #setAuthentication */ public boolean authenticate(String user, String pass) throws IOException { switch (authentication) { case AUTO: if (apop(user, pass)) { return true; } case USER: return userPass(user, pass); case APOP: return apop(user, pass); } return false; } private boolean userPass(String user, String pass) throws IOException { sendCommand("USER " + user); if (getResponse()) { sendCommand("PASS " + pass); if (getResponse()) { state = TRANSACTION; return true; } } return false; } private boolean apop(String user, String pass) throws IOException { if (nonce != null) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(nonce.getBytes()); if (pass == null) { pass = ""; } byte[] digest = md.digest(pass.getBytes()); sendCommand("APOP " + user + " " + digestToString(digest)); if (getResponse()) { return true; } } catch (NoSuchAlgorithmException e) { debugOut.println("NoSuchAlgorithmException: " + e.getMessage()); } } return false; } /** * Check the status of a user's maildrop. This sends the STAT * command to the POP3 server. If the command succeeds, you can * retrieve the number of messages available and the total size * in octets with <CODE>getTotalMessages</CODE> and * <CODE>getTotalOctets</CODE>. * * @return true if successful, false if not * @exception IOException if an input or output exception occurred * @see #getTotalMessages * @see #getTotalOctets */ public boolean status() throws IOException { totalMessages = -1; totalOctets = -1; sendCommand("STAT"); if (getResponse()) { int status[] = new int[2]; status = parseStatus(response.substring(4)); totalMessages = status[0]; totalOctets = status[1]; return true; } else { return false; } } private int[] parseStatus(String s) { int status[] = new int[2]; try { status[0] = Integer.parseInt(s.substring(0, s.indexOf(' '))); s = s.substring(s.indexOf(' ') + 1); int i = s.indexOf(' '); if (i != -1) { s = s.substring(0, i); } status[1] = Integer.parseInt(s); } catch (NumberFormatException e) { debugOut.println("NumberFormatException: " + e.getMessage()); } return status; } /** * Get the total number of messages available for retrieval. * The <CODE>status</CODE> method needs to be invoked before * this method will return any useful value. * * @return the number of messages available for retrieval or * -1 if the <CODE>status</CODE> method hasn't been called or * an error occured when it was called * @see #status */ public int getTotalMessages() { return totalMessages; } /** * Get the total size in octets of all the available messages. * The <CODE>status</CODE> method needs to be invoked before * this method will return any useful value. * * @return the size in octets of all the available messages * or -1 is the <CODE>status</CODE> method hasn't been called * or an error occured when it was * @see #status */ public int getTotalOctets() { return totalOctets; } /** * Return a Vector containing the size of each available * message in octets. The Vector will contain as many elements * as the highest numbered message on the server. Messages marked * for deletion will be represented by an Integer object with a * value of -1. For example, if a session started out with 3 * messages availble and message 2 was deleted, the resulting * vector would contain [message 1's size, -1, message 3's size]. * * @return true if the command succeeded, false if not * @exception IOException if an input or output exception occurred * @see #getOctetsVector */ public boolean list() throws IOException { octetsVector = new Vector(); sendCommand("LIST"); if (getResponse()) { String s; int status[] = new int[2]; int i = 1; do { s = getLine(); if (!s.equals(".")) { status = parseStatus(s); if (status[0] != i) { for (int j = i; j < status[0]; ++j) { octetsVector.addElement(new Integer(-1)); } } octetsVector.addElement(new Integer(status[1])); i = status[0] + 1; } } while (!response.equals("."));
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?