?? pop3client.java
字號:
/**
* POP3Client.java
*
* Copyright (C) 1998-2000 FreeBeans <freebeans@mub.biglobe.ne.jp>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) 1998-2000? FreeBeans <freebeans@mub.biglobe.ne.jp>
*
* ?????????????????????????Free Software Foundation
* ?????GNU ??????????????????????????????
* ?????????????????????????????????????
* ???????????????????
* ?????????????????????????????????????
* ?????????????????????????????????????
* GNU ??????????????????
*
* ???????????????GNU ??????????????????
* ???????????????Free Software Foundation, Inc., 675 Mass Ave,
* Cambridge, MA 02139, USA ????????????
*/
package jp.gr.java_conf.roadster.net.pop;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.SocketException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.net.ssl.SSLSocketFactory;
/**
* POP3?????????????????.
* POP3???????????????????.<BR>
* ???????javax.mail.* ??????????????????.
* <BR>
* ???????????????????.<BR>
* <OL>
* <LI>POP3Client????????????????????????
* ?????.<BR>
* <UL><CODE>
* POP3Client pc = new POP3Client();<BR>
* pc.setHost("pop3.host.ne.jp");<BR>
* pc.setUser("user");<BR>
* pc.setPassword("password");<BR>
* pc.setAuthorizationMode(POP3Client.APOP_AUTHORIZATION);<BR>
* </CODE></UL>
* <LI>????????.<BR>
* <UL><CODE>
* pc.connect();<BR>
* </CODE></UL>
* <LI>????????????.<BR>
* <UL><CODE>
* int count = pc.getMessageCount();<BR>
* </CODE></UL>
* <LI>?????????????????????????.<BR>
* <UL><CODE>
* InputStream ins = pc.retrieve(i); // i ???????.<BR>
* while ((c = ins.read()) != -1) {
* <UL>
* // ?????????????.
* </UL>
* }<BR>
* ins.close();<BR>
* </CODE></UL>
* <LI>???????????????.
* <UL><CODE>
* pc.delete(i); // i ???????.<BR>
* </CODE></UL>
* <LI>????????????.<BR>
* <UL><CODE>
* pc.disconnect();<BR>
* </CODE></UL>
* </OL>
*
* ??????????????????????????????.<BR>
* <OL>
* <LI>TCP/IP??????????????(greeting)????????.
* <LI>USER?????PASS??????????????.<BR>
* ????APOP???????????.
* <LI>LIST ??????????????????????????.<BR>
* +OK ...<BR>
* 1 203[CRLF]<BR>
* 2 345[CRLF]<BR>
* (??)<BR>
* .[CRLF]<BR>
* <LI>????????????????????512 ??????????.<BR>
* ????RETR ?????????????????????????????.
* </OL>
*
*/
public class POP3Client implements Serializable {
/**
* ???????????.
*/
static final long serialVersionUID = -3928372169782263062L;
/**
* ??????????( = 512???).
* ????????????????????.
*/
private static final int MAX_LINE_LENGTH = 512;
/**
* ??????????????????.
*/
private static final String ENCODE = "8859_1";
/**
* ??????POP???(=110)
*/
protected static final int DEFAULT_POP3_PORT = 110;
/**
* CR???.
*/
protected static final byte CR = 0x0d;
/**
* LF???.
*/
protected static final byte LF = 0x0a;
/**
* USER ????.
*/
protected static final String COMMAND_USER = "USER";
/**
* PASS ????.
*/
protected static final String COMMAND_PASS = "PASS";
/**
* QUIT ????.
*/
protected static final String COMMAND_QUIT = "QUIT";
/**
* STAT ????.
*/
protected static final String COMMAND_STAT = "STAT";
/**
* LIST ????.
*/
protected static final String COMMAND_LIST = "LIST";
/**
* RETR ????.
*/
protected static final String COMMAND_RETR = "RETR";
/**
* DELE ????.
*/
protected static final String COMMAND_DELE = "DELE";
/**
* NOOP ????.
*/
protected static final String COMMAND_NOOP = "NOOP";
/**
* RSET ????.
*/
protected static final String COMMAND_RSET = "RSET";
/**
* APOP ????.
*/
protected static final String COMMAND_APOP = "APOP";
/**
* TOP ????.
*/
protected static final String COMMAND_TOP = "TOP";
/**
* UIDL ????.
*/
protected static final String COMMAND_UIDL = "UIDL";
/**
* ????.
*/
protected static final String RESPONSE_OK = "+OK";
/**
* ?????.
*/
protected static final String RESPONSE_ERR = "-ERR";
/**
* ????????(USER/PASS ???????????
*
* @see #setAuthorizationMode(int)
*/
public static final int NORMAL_AUTHORIZATION = 1;
/**
* APOP?????.
*
* @see #setAuthorizationMode(int)
*/
public static final int APOP_AUTHORIZATION = 2;
/**
* @serial POP????. ????????????????.
*/
private String user;
/**
* ?????.
* ????????????????????? transient ???.
*/
private transient String password;
/**
* @serial POP???????.
*/
private String host;
/**
* @serial POP????????.
*/
private int port = -1;
/**
* @serial ?????. ?????? NORMAL_AUTHORIZATION
*/
private int authorizationMode = NORMAL_AUTHORIZATION;
/**
* ???????.
*/
protected transient Socket socket;
/**
* ???????????? Stream.
*/
protected transient InputStream in;
/**
* ??????????? Stream.
*/
protected transient OutputStream out;
/**
* ????????????????????.
*/
protected transient boolean connected = false;
/**
* ??????????????????.
*/
private transient byte responseBytes[];
/**
* ????????????????????????????.
*/
private transient String status;
/**
* ??????????????????(greeting).
*/
private transient String greeting;
/**
* LIST ????????????????????.
*/
protected transient String messageList[] = null;
/**
* UIDL ????????????????????.
*/
protected transient String uidList[] = null;
/**
* ????????????????????.
*/
protected transient POP3MessageInputStream messageInput = null;
/**
* @serial ???????.
*/
private boolean debug;
/**
* @serial ?????????
*/
protected int timeout;
/**
* @serial ?????????????
*/
private String preConnectCommand;
/**
* 是否使用ssl
*/
private boolean ssl;
/**
* ???????????POP3Client????????????.
*/
public POP3Client() {
// ?????.
}
/**
* ?????????????????????? POP3Client????????????.
*
* @param host ????.
* @exception java.lang.IllegalArgumentException host ? null ???.
*/
public POP3Client(String host) {
this(host, DEFAULT_POP3_PORT);
}
/**
* ????????????????? POP3Client????????????.
*
* @param host ????.
* @param port ?????.
* @exception java.lang.IllegalArgumentException host ? null ???.
*/
public POP3Client(String host, int port) {
setHost(host);
setPort(port);
}
/**
* ??????????????.
* ???????????????System.err ???.
*
* @param msg ???????.
*/
protected void debugOut(String msg) {
if (debug) {
StringBuffer buff = new StringBuffer("DEBUG:");
buff.append(msg);
System.err.println(buff.toString());
}
}
public void setDebug(boolean mode) {
this.debug = mode;
}
public boolean getDebug() {
return debug;
}
/**
* ??????????????????.
* CRLF ?????????.
*
* @return ????????????????.<BR>
* ???????CRLF??????????????.
* @exception java.io.IOException IO??????????.
*/
protected synchronized String readResponseLine() throws IOException {
boolean cr = false;
int i;
for (i = 0; i < responseBytes.length; ++i) {
int c = in.read();
if (c == -1) {
break;
}
responseBytes[i] = (byte) c;
if (responseBytes[i] == CR) {
cr = true;
} else if (responseBytes[i] == LF && cr == true) {
break;
} else {
cr = false;
}
}
if( i== 0)
return "+OK";
return new String(responseBytes, 0, i - 1, ENCODE);
}
/**
* ?????TCP/IP???????.
* ?????????????????????????????.<BR>
*
* @exception java.io.IOException IO??????????.
* @exception java.lang.IllegalStateException ???????????????.
*/
protected synchronized void openConnection() throws IOException {
if (isConnected()) {
throw new IllegalStateException("Already connected.");
}
String cmd = getPreConnectCommand();
if (cmd != null) {
debugOut("Execute preconnect command:" + cmd);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -