[twsocket] Pop3Cli

Stephen Williams SWilliams@fm.optus.net.au
Mon, 5 Jun 2000 11:41:26 +1000


> How do I download the messages?  How do I extract data from the
> "headers/properties"? Where are the "headers/properties"?

Here is a basic skeleton for downloading all messages from a maildrop and
storing each message in its own file using the message UIDL as the filename.
Note that using the UIDL will not always be suitable for filenames, so it
would be better to come up with your own naming scheme.

I'll leave it as an exercise for you to work out how to extract the header
information.  I would recommend reading the RFC on standard Internet message
format.  I think this is RFC822 from memory.  Anyone care to correct me on
that?  If you still have trouble with the headers after a few days, mail the
list again and we'll help you out.

Note that this code is all off the top of my head (once again :), but
hopefully it will compile and work first go.

(Is it a bad thing that I have memorised this piece of code?  :)

procedure TForm1.StartButton(Sender: TObject);
begin
  Pop3Cli1.Host := HostEdit.Text;
  Pop3Cli1.Port := PortEdit.Text;
  Pop3Cli1.Connect;
end;

procedure TForm1.Pop3Cli1RequestDone(Sender: TObject; RqType: TPop3Request;
  Error: Word);
begin
  if Error <> 0 then
  begin
    if Error < 10000 then
    begin
      { Protocol error, so quit. }
      TPop3Cli(Sender).Quit;
      raise Exception.Create(TPop3Cli(Sender).LastResponse);
    end
    else
    begin
      { Winsock error, so abort. }
      TPop3Cli(Sender).Abort;
      raise Exception.Create(WSocketErrorDesc(Error));
    end;
  end;
  case RqType of
    pop3Connect:
      begin
        TPop3Cli(Sender).UserName := UserNameEdit.Text;
        TPop3Cli(Sender).User;
      end;
    pop3User:
      begin
        TPop3Cli(Sender).PassWord := PasswordEdit.Text;
        TPop3Cli(Sender).Pass;
      end;
    pop3Pass:
      begin
        { Let's see how many messages are waiting. }
        TPop3Cli(Sender).Stat;
      end;
    pop3Stat:
      begin
        { If there are messages to retrieve, then get them. }
        if TPop3Cli(Sender).MsgCount > 0 then
        begin
          TPop3Cli(Sender).MsgNum := 1;
          TPop3Cli(Sender).Uidl;
        end
        else
        begin
          TPop3Cli(Sender).Quit;
        end;
      end;
    pop3Uidl:
      begin
        { Now we have the message UIDL, retrieve the message itself. }
        TPop3Cli(Sender).Retr;
      end;
    pop3Retr:
      begin
        { Do we need to retrieve more messages? }
        if TPop3Cli(Sender).MsgNum < TPop3Cli(Sender).MsgCount then
        begin
          TPop3Cli(Sender).MsgNum := TPop3Cli(Sender).MsgNum + 1;
          TPop3Cli(Sender).Uidl;
        end
        else
        begin
          TPop3Cli(Sender).Quit;
        end;
      end;
    pop3Quit:
      begin
        { Ding a bell?  Set the statusbar text?  Whatever you want. }
      end;
  end;
end;

procedure TForm1.Pop3Cli1MessageBegin(Sender: TObject);
begin
  { FMsgFile is a private field of type TextFile in the TForm1 object. }
  AssignFile(FMsgFile, ExtractFilePath(ParamStr[0]) +
TPop3Cli(Sender).MsgUidl);
  Rewrite(FMsgFile);
end;

procedure TForm1.Pop3Cli1MessageLine(Sender: TObject);
begin
  WriteLn(FMsgFile, TPop3Cli(Sender).LastResponse);
end;

procedure TForm1.Pop3Cli1MessageEnd(Sender: TObject);
begin
  CloseFile(FMsgFile);
end;

Steve 'Sly' Williams
IT Development
Cable & Wireless Optus Outsourcing Services
swilliams@fm.optus.net.au
------------------------------------------------------------
"And God said: 'Let there be Satan, so people don't blame everything on me.
And let there be lawyers, so people don't blame everything on Satan."
------------------------------------------------------------