📄 conpop3.dpr
字号:
{ This event handler is called for each list line received by TPop3Client. }
procedure TPOP3ExercizerApp.Pop3ClientListLine(Sender: TObject);
var
Buffer : String;
begin
Buffer := 'MsgNum = ' + IntToStr((Sender as TPop3Cli).MsgNum) + ' ' +
'MsgSize = ' + IntToStr((Sender as TPop3Cli).MsgSize) + ' ' +
'Line = ''' + (Sender as TPop3Cli).LastResponse + '''';
{$IFDEF DEBUG}
WriteLn(Buffer);
{$ENDIF}
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TPOP3ExercizerApp.Pop3ClientUidlBegin(Sender: TObject);
begin
{$IFDEF DEBUG}
WriteLn('*** Uidl begin ***');
{$ENDIF}
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TPOP3ExercizerApp.Pop3ClientUidlEnd(Sender: TObject);
begin
{$IFDEF DEBUG}
WriteLn('*** Uidl end ***');
{$ENDIF}
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TPOP3ExercizerApp.Pop3ClientUidlLine(Sender: TObject);
var
Buffer : String;
begin
Buffer := 'MsgNum = ' + IntToStr((Sender as TPop3Cli).MsgNum) + ' ' +
'MsgUidl = ' + (Sender as TPop3Cli).MsgUidl + '''';
{$IFDEF DEBUG}
WriteLn(Buffer);
{$ENDIF}
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TPOP3ExercizerApp.MessageBegin(Sender: TObject);
begin
{$IFDEF DEBUG}
WriteLn('Message ' +
IntToStr((Sender as TPop3Cli).MsgNum));
{$ENDIF}
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TPOP3ExercizerApp.MessageLine(Sender: TObject);
begin
{$IFDEF DEBUG}
WriteLn((Sender as TPop3Cli).LastResponse);
{$ENDIF}
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TPOP3ExercizerApp.Nextevent(Sender: TObject);
begin
Pop3Client.OnMessageBegin := MessageBegin;
Pop3Client.OnMessageEnd := nil;
Pop3Client.OnMessageLine := MessageLine;
Pop3Client.OnRequestDone := NextMessageRequestDone;
Pop3Client.MsgNum := MsgNum;
Pop3Client.Retr;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TPOP3ExercizerApp.NextMessageRequestDone(
Sender : TObject;
RqType : TPop3Request;
ErrCode : Word);
begin
if ErrCode <> 0 then
Exit;
Inc(MsgNum);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TPOP3ExercizerApp.GetAllMessageLine(Sender: TObject);
begin
WriteLn(FFile, (Sender as TPop3Cli).LastResponse);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ The procedure here after will start an event chain that will eventually }
{ download all messages for the POP3 server. We cannot simply loop because }
{ the POP3 compomnet is asynchronous: it will not wait for operation done }
{ before returning. We must "chain" operations one after the other using }
{ the OnRequestDone event handler. We use the variable FGetAllState to keep }
{ track of where we are. }
{ To get all messages, we must first call Stat to know how many messages }
{ are on the server, then for each message we call Uidl to get a unique }
{ identifier for each message to build a file name and know if we already }
{ have a message, then we retrieve the message, then we increment the }
{ message number and continue until the number of messages is reached. }
{ We should start a TTimer to handle timeout... }
procedure TPOP3ExercizerApp.GetAllevent(Sender: TObject);
var
IniFile : TIniFile;
begin
{ Get path from INI file }
IniFile := TIniFile.Create(IniFileName);
FMsgPath := IniFile.ReadString(SectionData, KeyMsgPath,
ExtractFilePath(ParamStr(0)));
IniFile.Free;
{ Be sure to have an ending backslash }
if (Length(FMsgPath) > 0) and (FMsgPath[Length(FMsgPath)] <> '\') then
FMsgPath := FMsgPath + '\';
FGetAllState := 0;
FFileOpened := FALSE;
Pop3Client.OnRequestDone := GetAllRequestDone;
Pop3Client.OnMessageBegin := nil;
Pop3Client.OnMessageEnd := nil;
Pop3Client.OnMessageLine := GetAllMessageLine;
Pop3Client.Stat;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ This event handler is called when a request related to GetAll is done. }
{ We check for errors and our state variable FGetAllState which tells us }
{ where we are (stat, uidl or retr which are the 4 commands we use. }
{ Note that we also could use Dele to remove the messages from the server. }
procedure TPOP3ExercizerApp.GetAllRequestDone(
Sender: TObject;
RqType: TPop3Request; ErrCode: Word);
begin
if ErrCode <> 0 then begin
if FFileOpened then begin
FFileOpened := FALSE;
CloseFile(FFile);
end;
WriteLn('Error ' + Pop3Client.ErrorMessage);
Exit;
end;
try
case FGetAllState of
0: begin { Comes from the Stat command }
if Pop3Client.MsgCount < 1 then begin
WriteLn('No message to download.');
Exit;
end;
Pop3Client.MsgNum := 1; { Start with first message }
FGetAllState := 1;
Pop3Client.Uidl;
end;
1: begin { Comes from the Uidl command }
FFileName := FMsgPath + 'Msg ' + Pop3Client.MsgUidl + '.txt';
if FileExists(FFileName) then begin
WriteLn('Message ' + IntToStr(Pop3Client.MsgNum) + ' already here');
if Pop3Client.MsgNum >= Pop3Client.MsgCount then begin
WriteLn('Finished');
Exit;
end;
Pop3Client.MsgNum := Pop3Client.MsgNum + 1;
FGetAllState := 1;
Pop3Client.Uidl;
end
else begin
WriteLn('Message ' + IntToStr(Pop3Client.MsgNum));
AssignFile(FFile, FFileName);
Rewrite(FFile);
FFileOpened := TRUE;
FGetAllState := 2;
Pop3Client.Retr;
end;
end;
2: begin { Comes from the Retr command }
FFileOpened := FALSE;
CloseFile(FFile);
if Pop3Client.MsgNum >= Pop3Client.MsgCount then begin
WriteLn('Finished');
Exit;
end;
Pop3Client.MsgNum := Pop3Client.MsgNum + 1;
FGetAllState := 1;
Pop3Client.Uidl;
end;
else
WriteLn('Invalid state');
Exit;
end;
except
on E:Exception do begin
if FFileOpened then begin
FFileOpened := FALSE;
CloseFile(FFile);
end;
WriteLn('Error: ' + E.Message);
end;
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TPOP3ExercizerApp.Pop3ClientRequestDone(
Sender : TObject;
RqType : TPop3Request;
ErrCode : Word);
begin
WaitFor:=rqtype;
{$IFDEF DEBUG}
WriteLn('Request Done Rq=' + IntToStr(Integer(RqType)) +
' Error=' + IntToStr(ErrCode));
{$ENDIF}
if RqType = pop3Stat then begin
MsgCount:=Pop3Client.MsgCount;
WriteLn('Stat ok, ' +
IntToStr(Pop3Client.MsgCount) + ' messages ' +
IntToStr(Pop3Client.MsgSize) + ' bytes');
end
else if RqType = pop3List then begin
WriteLn('List ok, ' +
IntToStr(Pop3Client.MsgNum) + ' message ' +
IntToStr(Pop3Client.MsgSize) + ' bytes');
end
else if RqType = pop3Last then begin
WriteLn('Last = ' + IntToStr(Pop3Client.MsgNum));
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TPOP3ExercizerApp.Pop3ClientHeaderEnd(Sender: TObject);
begin
// SubjectEdit.Text := Pop3Client.HeaderSubject;
// FromEdit.Text := Pop3Client.HeaderFrom;
// ToEdit.Text := Pop3Client.HeaderTo;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TPOP3ExercizerApp.Wait;
var
St : TPop3State;
begin
St := Pop3Client.State;
WriteLn('pre state: ', ORD(Pop3Client.state));
while Pop3Client.State = St do
ProcessMessages;
WriteLn('post state: ', ORD(Pop3Client.state));
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TPOP3ExercizerApp.WaitReady;
var
Current: TPop3State;
begin
Current:=Pop3Client.state;
while (Pop3Client.State<>Pop3Ready) do begin
if Current<>Pop3Client.State then begin
Current := Pop3Client.State;
WriteLn('stage change: ',ORD(current));
end;
ProcessMessages;
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TPOP3ExercizerApp.waitfordone(ev: TPop3Request);
begin
while (Waitfor <> ev) and (Pop3Client.State <> Pop3Abort) do
ProcessMessages;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
var
App : TPOP3ExercizerApp;
I : Integer;
begin
WriteLn(Trim(CopyRight));
App := TPOP3ExercizerApp.Create(nil);
App.ReadDataFromIni(nil);
WriteLn('Connecting to: ',
App.FHost, '/', App.FPort, ' user/pw=',
App.FUser, '/', App.FPasswd);
while TRUE do begin
if not App.ConnectSyncEvent(App) then
break;
if not App.UserSyncEvent(App) then
break;
if not App.PassSyncEvent(App) then
break;
if not App.StatSyncEvent(App) then // loads nr of msgs in App.MsgCount
break;
{$IFDEF DOHEADERS}
App.OkMsgNumber.Clear;
App.Lines := 0; // Iterate through all headers.
WriteLn(App.msgcount);
WriteLn(App.OkMsgNumber.Count);
if App.MsgCount > 0 then begin
for I := 1 to App.MsgCount do begin
App.msgnum := I;
App.TopSyncEvent(App);
end;
end;
if App.OkMsgNumber.Count > 0 then begin
for I:=0 to App.OkMsgNumber.Count - 1 do begin
App.msgnum := Integer(App.OkMsgNumber[I]); // no beauty either
App.RetrSyncEvent(App);
end;
end;
{$ELSE}
App.ListAllEvent(App); // async is also possible.
App.WaitForDone(Pop3list); // but for the linear sequence like this not interesting.
{$ENDIF}
end;
App.AbortEvent(App);
App.Free;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -