?????? 找一個開源項目來學(xué)習(xí)Java代碼,自己先讀一下,然后按自己的思路來重復(fù)實現(xiàn)其中一些功能!
一.POP3
協(xié)議
1.??????
目前的電子郵件基本上都是通過
POP3
網(wǎng)絡(luò)協(xié)議接收的。建立雙向的傳輸通道以后,
pop3
服務(wù)程序會發(fā)送一系列基于
ASCII
字符的命令,下面的圖大概說明了一下!
通常在學(xué)習(xí)的過程中,都是去寫
email
客戶端,其實一個方面是去寫一些處理的
function
;另一方面基本上就是寫一個實現(xiàn)
pop3
協(xié)議的包。
?? 常用的命令
USER,PASS,STAT,RETR,DELE
和
QUIT. POP3
中只有兩種回應(yīng)碼
”+OK”
和
”-ERR”
。詳細?
?????? 的東西可以找RFC文檔來仔細定義.
2.??????
package org.columba.ristretto.pop3
該包實現(xiàn)了
RFC1939
所指定的
POP3
協(xié)議(
Post Office Protocol Version3
)
3.??????
在分析這個包之前,我們必須意識到一個完整的軟件通常都會自己去實現(xiàn)一些輔助的功能,該開源項目里自己實現(xiàn)了一些
I/O
功能。(不過這里有個疑問啊,這樣去實現(xiàn)一些東西,對于自己的一些應(yīng)用是方便了,可是對于其他想復(fù)用這些代碼的人來說,就需要更多的學(xué)習(xí)時間)
?? 接口
Source
繼承自
CharSequence
?? 接口
Steamable
只有一個函數(shù)可以返回數(shù)據(jù)庫結(jié)構(gòu)的
InputStream?
?4.? 這里有一個可以大概實現(xiàn)pop3client的例子,抄來的,很簡單不過可以實現(xiàn)基本的pop3client功能,對協(xié)議的理解也小有幫助!
???? import java.io.*;
import java.net.*;
class POP3Demo
{
?public static void main (String[] args)
?{
??String POP3Server = "server address";
??int POP3Port = 110;
??Socket clientsocket = null;
??try
??{
???clientsocket = new Socket(POP3Server, POP3Port);
???BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
???
???InputStream is =clientsocket.getInputStream();
???BufferedReader sockin = new BufferedReader(new InputStreamReader(is));
???
???OutputStream os = clientsocket.getOutputStream();
???PrintWriter sockout = new PrintWriter(os,true);
???
???System.out.println("S:"+sockin.readLine());
???while(true)
???{
????System.out.print("C:");
????String cmd = stdin.readLine();
????sockout.println(cmd);
????
????String reply = sockin.readLine();
????System.out.println("S:" + reply);
????
????if(cmd.toLowerCase().startsWith("retr")&&reply.charAt(0)=='+')
????do
????{
?????reply = sockin.readLine();
?????System.out.println("S:"+reply);
?????if(reply!=null && reply.length() > 0)
?????if(reply.charAt(0) == '.')
?????break;
????}
????while(true);
????
????if(cmd.toLowerCase().startsWith("quit"))
????break;
????
???}
??}
??catch(IOException e)
??{
???System.out.println(e.toString());
??}
??finally
??{
???try
???{
????if(clientsocket!=null)
????clientsocket.close();
???}
???catch(IOException e)
???{
???}
??}??
?}
}