一直在侦听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服务器吧!