• <label id="pxtpz"><meter id="pxtpz"></meter></label>
      1. <span id="pxtpz"><optgroup id="pxtpz"></optgroup></span>

        當前位置:雨林木風下載站 > 技術開發教程 > 詳細頁面

        Email 服務器的容易完成

        Email 服務器的容易完成

        更新時間:2022-04-29 文章作者:未知 信息來源:網絡 閱讀次數:

        我們知道從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服務器吧! 

        溫馨提示:喜歡本站的話,請收藏一下本站!

        本類教程下載

        系統下載排行

        主站蜘蛛池模板: 亚洲综合无码AV一区二区| 女人18毛片水最多免费观看 | 国产h视频在线观看网站免费| 亚洲熟妇无码八AV在线播放| 无遮挡a级毛片免费看| 国产国产人免费视频成69大陆| 亚洲乱妇老熟女爽到高潮的片| 毛片在线看免费版| 中文日韩亚洲欧美制服| 最新欧洲大片免费在线 | 成人A毛片免费观看网站| 精品亚洲一区二区三区在线观看 | 亚洲天堂福利视频| 久草在视频免费福利| 亚洲中文字幕无码爆乳app| 成人人免费夜夜视频观看| 色婷婷六月亚洲综合香蕉| 亚洲高清国产拍精品青青草原| 一级一级一级毛片免费毛片| 色噜噜AV亚洲色一区二区| 秋霞人成在线观看免费视频| 亚洲精品在线免费观看视频| 毛片a级毛片免费观看免下载| 亚洲av午夜电影在线观看 | 亚洲国产精品综合久久一线| 人与动性xxxxx免费| 亚洲欧洲在线观看| 毛片视频免费观看| 乱淫片免费影院观看| 99久久亚洲精品无码毛片| AV免费网址在线观看| 免费无遮挡无遮羞在线看| 亚洲AV永久无码精品一百度影院| 最近2019免费中文字幕6| 亚洲欧美国产日韩av野草社区| 丁香亚洲综合五月天婷婷| 久9热免费精品视频在线观看| 亚洲一级片在线观看| 亚洲精品国产高清嫩草影院 | 大学生一级毛片免费看| 免费无毒a网站在线观看|