有發送有接收才顯得完整一點。
同樣分兩部分寫:
一:POP3命令介紹(抄的);二:執行個體。
一:POP3命令介紹
telnet 119.119.119.212 110 ----------------------------- 使用 telnet 命令串連伺服器 110 連接埠
Trying 119.119.119.212... ------------------------------ 正在串連伺服器 110 連接埠
Connected to 119.119.119.212. -------------------------- 串連伺服器 110 連接埠成功
+OK Winmail Mail Server POP3 ready
user username ------------------------------------------ 輸入使用者名稱, username 為具體的使用者名稱
+OK ---------------------------------------------------- 執行命令成功
pass password ------------------------------------------ 輸入使用者密碼,password 為具體的密碼
+OK 2 messages ----------------------------------------- 密碼認證通過
(-ERR authorization failed ----------------------------- 密碼認證失敗)
stat --------------------------------------------------- 郵箱狀態
+OK 2 6415 --------------------------------------------- 2 為該信箱總郵件數,6415 為總位元組數
list --------------------------------------------------- 列出每封郵件的位元組數
+OK ---------------------------------------------------- 執行命令成功,開始顯示,左邊為郵件的序號,右邊為該郵件的大小
1 537 -------------------------------------------------- 第 1 封郵件,大小為 537 位元組
2 5878 ------------------------------------------------- 第 2 封郵件,大小為 5878 位元組
.
top 1 -------------------------------------------------- 接收第 1 封郵件
+OK ---------------------------------------------------- 接收成功, 返回第 1 封郵件標頭
Return-Path: <test1@look.com>
Delivered-To: test2@look.com
Received: (winmail server invoked for smtp delivery); Mon, 25 Oct 2004 14:24:27 +0800
From: test1@look.com
To: test2@look.com
Date: Mon, 25 Oct 2004 14:24:27 +0800
Subject: test mail
.
retr 1 ------------------------------------------------- 接收第 1 封郵件
+OK ---------------------------------------------------- 接收成功, 返回第 1 封郵件全部內容
Return-Path: <test1@look.com>
Delivered-To: test2@look.com
Received: (winmail server invoked for smtp delivery); Mon, 25 Oct 2004 14:24:27 +0800
From: test1@look.com
To: test2@look.com
Date: Mon, 25 Oct 2004 14:24:27 +0800
Subject: test mail
Hi, test2
This is a test mail, you don't reply it.
.
dele 1 ------------------------------------------------- 刪除第 1 封郵件
+OK ---------------------------------------------------- 刪除成功
dele 2 ------------------------------------------------- 刪除第 2 封郵件
+OK ---------------------------------------------------- 刪除成功
quit --------------------------------------------------- 結束會話
+OK ---------------------------------------------------- 執行命令成功
二:發送郵件執行個體
package mail;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;
public class POP3Client {
private Socket socket;
private boolean debug=true;
public static void main(String[] args) throws UnknownHostException, IOException {
String server="pop3.126.com";//POP3伺服器位址
String user="wasingmon";//使用者名稱
String password="";//密碼
POP3Client pop3Client=new POP3Client(server,110);
pop3Client.recieveMail(user,password);
}
public POP3Client(String server,int port) throws UnknownHostException, IOException{
try{
socket=new Socket(server,port);
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("建立串連!");
}
}
//得到伺服器返回的一行命令
public String getReturn(BufferedReader in){
String line="";
try{
line=in.readLine();
if(debug){
System.out.println("伺服器返回狀態:"+line);
}
}catch(Exception e){
e.printStackTrace();
}
return line;
}
//從返回的命令中得到第一個欄位,也就是伺服器的返回狀態代碼(+OK或者-ERR)
public String getResult(String line){
StringTokenizer st=new StringTokenizer(line," ");
return st.nextToken();
}
//發送命令
private String sendServer(String str,BufferedReader in,BufferedWriter out) throws IOException{
out.write(str);
out.newLine();
out.flush();
if(debug)
{
System.out.println("已發送命令:"+str);
}
return getReturn(in);
}
//user命令
public void user(String user,BufferedReader in,BufferedWriter out) throws IOException{
String result;
result=getResult(getReturn(in));
if(!"+OK".equals(result)){
throw new IOException("串連伺服器失敗!");
}
result=getResult(sendServer("user "+user,in,out));
if(!"+OK".equals(result)){
throw new IOException("使用者名稱錯誤!");
}
}
//pass命令
public void pass(String password,BufferedReader in,BufferedWriter out) throws IOException{
String result;
result=getResult(sendServer("pass "+password,in,out));
if(!"+OK".equals(result)){
throw new IOException("密碼錯誤!");
}
}
//stat命令
public int stat(BufferedReader in,BufferedWriter out) throws IOException{
String result;
String line;
int mailNum;
line=sendServer("stat",in,out);
StringTokenizer st=new StringTokenizer(line," ");
result=st.nextToken();
if(st.hasMoreTokens())
mailNum=Integer.parseInt(st.nextToken());
else
mailNum=0;
if(!"+OK".equals(result)){
throw new IOException("查看郵箱狀態出錯!");
}
System.out.println("共有郵件"+mailNum+"封");
return mailNum;
}
//得到郵件詳細資料
public String getMessagedetail(BufferedReader in){
String message="";
String line;
try{
line=in.readLine();
while(!".".equalsIgnoreCase(line)){
message=message+line+"/n";
line=in.readLine();
}
}catch(Exception e){
e.printStackTrace();
}
return message;
}
//retr命令
public void retr(int mailNum,BufferedReader in,BufferedWriter out) throws IOException{
String result;
for(int i=1;i<=mailNum;i++){
result=getResult(sendServer("retr "+i,in,out));
if(!"+OK".equals(result)){
throw new IOException("內送郵件出錯!");
}
System.out.println("第"+i+"封");
System.out.println(getMessagedetail(in));
}
}
//退出
public void quit(BufferedReader in,BufferedWriter out) throws IOException{
String result;
result=getResult(sendServer("QUIT",in,out));
if(!"+OK".equals(result)){
throw new IOException("未能正確退出");
}
}
//內送郵件程式
public boolean recieveMail(String user,String password){
try{
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
user(user,in,out);
pass(password,in,out);
int mailNum;
mailNum=stat(in,out);
retr(mailNum,in,out);
quit(in,out);
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
}