Email 服務器的容易完成
發表時間:2023-07-21 來源:明輝站整理相關軟件相關文章人氣:
[摘要]我們知道從Delphi 3 開始,它自帶的控件中有基于Internet開發的控件。如果我們充分利用這些控件開發Internet程序則可以簡化編程工作,提高效率。鑒于目前POP3客戶端的軟件的種類繁多...
我們知道從Delphi 3 開始,它自帶的控件中有基于Internet開發的控件。如果我們充分利用這些控件開發Internet程序則可以簡化編程工作,提高效率。鑒于目前POP3客戶端的軟件的種類繁多,(如Outlook Express,Foxmail 以及Web 方式下的各免費郵局),而服務器端(除Unix Email系統)很少公開原代碼,下面我就向大家著重介紹一下利用 Delphi 4中Internet控件組的TClientSocket 和TServerSocket 控件來實現 Email POP3服務器端。如果您理解了Email POP3服務器的構造,相信也可以依葫蘆畫瓢寫出Email SMTP服務器程序。在此基礎上加入多線程技術使服務器能同時處理多個客戶的連接請求,您就可以輕松地實現一個簡單的Email服務器了。
一. 設計思路
Email 系統采用C/S 結構。當用戶想發送郵件時或收取郵件時在客戶機上運行任意一個客戶端程序,如Foxmail。在菜單’工具->選項’的郵件服務器里填上運行我們服務器程序的主機名。服務器主機24小時一直運行我們的服務器端程序,SMTP和POP3服務器程序分別在25端口和110端口偵聽連接請求。當用戶發信時,首先客戶端會與服務器端建立Socket連接。然后開始一應一答的Client/Server間的通信。發信和收信時建立連接后服務器端分別要發送一個’250 OK’ 和’+OK pop3 server is ready ’的應答。客戶端收到此應答后開始發送SMTP或POP3命令。POP3通信時一般最開始的命令是’user ‘和’pass’或’ apop’用以進行身份驗證。注意由于POP3會話有3個狀態,某些命令只在某特定狀態下有效。當用戶進行完所有的操作后發送一個’quit’命令。服務器端收到此命令即終止此次socket連接并繼續偵聽其他的連接請求。注意:POP3通信時客戶端在Transaction狀態下’quit’則進入update狀態。如果從Authorization狀態下’quit’則終止通信,而不進入Update狀態。如果客戶端不通過’quit’命令終止連接,POP3會話不會進入Update狀態。而只有在Update狀態下收到’quit’命令后服務器才會在斷連前把標志為已刪的郵件進行物理刪除。
二. 代碼實現(以POP3為例)
自定義TPOP類的描述:
SessionState = ( Init,Authorization, Transaction,Update);
TPop=class (TComponent)
public
UserName:string;//Email帳戶名
PassWord:string; //Email口令
ReceText:Pchar; //server端收到的字符串
PopState:SessionState;
//pop狀態:
init or authorization or transaction or update
MsgCount:integer; //郵件總數
SizeCount:integer; //郵件總大小
ReplyString:string;//服務器端發送的應答信息
DeleIndex:byte;//用戶要刪的郵件序號
ListIndex:byte;//list方法 的參數:
用戶要列出的序號為listindex的郵件信息
RetrIndex:byte;//retr方法的參數:
用戶要取序號為retrindex的郵件
TopIndex:byte; //top方法的參數
QuitFlag:boolean;//用戶如果通過quit命斷連則此變量為true;
反之(此時要把f_dele都置回0)
OldMsgCount:integer;//舊郵件數:Last 命令返回
//郵件頭的各個域
HMsgId:string;
HReplyTo:string;
HDate:string;
HFrom:string;
HTo:string;
HSubject:string;
HMIME_Ver:real;
HContent_Type:string;
HContent_Transfer_Encoding:string;
HText:string;
//所有POP3服務器必須支持的命令
procedure user;
function pass:boolean;
procedure stat;
procedure dele;
procedure list;
procedure retr;
procedure noop;
procedure rset;
procedure aquit;
procedure tquit;
//擴展的可選擇實現的POP3 命令
procedure top;
procedure last;
procedure apop;
procedure uidl;
end;
1. 建立連接
我們可以看到利用了Tclientsocket后客戶端請求建立連接只需下面的代碼。
with ClientSocket do
begin
Host := Server;
Active := True;
end;
服務器端利用TserverSocket,一直在偵聽110端口,若客戶端有連接請求,則ServerSocketAccept事件會被激活,建立起連接。
procedure TMyForm.ServerSocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
Statusbar1.Panels[0].Text :=
'連接到 ' + Socket.RemoteAddress;
//pop對象初始化
pop:=TPop.Create(nil);
pop.PopState:=init;
pop.LoginResult:=false;
pop.QuitFlag:=false;
ServerSocket.Socket.Connections[0]
.sendtext('+OK ibonc pop3 server is ready'+crlf);
end;
2. 通信
服務器端收到客戶端發來的信息,則會激活ServerSocketClientRead事件,通過ServerSocket的Socket.ReceiveText可以得到信息的內容。
procedure TMyForm.ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var temp_command :string;
//存放接收到的命令行,并做去crlf的處理
begin
temp_command:=Socket.ReceiveText;
//to remove the crlf in command line
temp_command:=trim(copy(temp_command,1,
pos(crlf,temp_command)-1));
pop.ReceText:=pchar(temp_command);
if pop.popstate=init then
if strLIComp(pop.ReceText,'user ',5)=0 then
pop.user
else
ServerSocket.Socket.Connections[0]
.sendtext('-ERR user name please')
else if pop.popstate=authorization then
begin
if strLIComp(pop.ReceText,'pass ',5)=0 then
pop.pass
else if strIComp(pop.ReceText,'quit')=0 then
pop.aquit
else
ServerSocket.Socket.Connections[0]
.sendtext('-ERR pass word please');
end
else if pop.popstate=transaction then
begin
if strIComp(pop.ReceText,'stat')=0 then
pop.stat
else if strLIComp(pop.ReceText,'dele ',5)=0 then
pop.dele
else if strLIComp(pop.ReceText,'list',4)=0 then
pop.list
else if strLIComp(pop.ReceText,'retr ',5)=0 then
pop.retr
else if strIComp(pop.ReceText,'noop')=0 then
pop.noop
else if strIComp(pop.ReceText,'rset')=0 then
pop.rset
else if strIComp(pop.ReceText,'quit')=0 then
pop.tquit
else if strIComp(pop.ReceText,'last')=0 then
pop.last
else if strLIComp(pop.ReceText, 'apop ',5)=0 then
pop.apop
else if strLIComp(pop.ReceText, 'uidl ',5)=0 then
pop.uidl
else
ServerSocket.socket.connections[0]
.sendtext('-ERR no such command yet'+crlf);
end
end;
3. 關閉連接
procedure TMyForm.ServerSocket
ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
ServerSocket.Active := False;
//如果client端沒有通過quit 命令斷連,
則在斷連時要把那些f_dele置為0
if pop.QuitFlag=False then
begin
MyForm.query11.Close;
MyForm.query11.Params[0].Asstring:=pop.UserName;
MyForm.query11.prepare;
MyForm.query11.execsql;
end;
end;
三. 結語
由于Email系統與數據庫表結構的緊密聯系,筆者沒有寫出各POP3命令的具體實現。相信讀者在認真閱讀了RFC1939之后不難寫出實現函數。現在就動手為你的公司寫一個自己的Email服務器吧!