[twsocket] SMTP: Why won't my code work?

Stephen Williams SWilliams@fm.optus.net.au
Fri, 23 Jun 2000 14:41:51 +1000


Ok, now that we know which particular component you are using and the code
that you are using (from your previous message), the message you would be
getting is a 'Component not ready' error.  This is because you are treating
the asynchronous TSmtpCli as if it were a synchronous component.

With asnychronous components in the ICS collection, you cannot call a method
and expect it to be completely finished by the time the next line gets
executed.  Your code must wait for the OnRequestDone event to be triggered
because only at this time can you be sure that the command has completed
(either successfully or unsuccessfully).

Here is a simple example of doing a complete mail from start to sent.

Pretend that the following controls exist and have these values:

FromEdit: TEdit
  Text := 'Raymond Kennington <raymondk@one.net.au>';
ToEdit: TEdit
  Text := 'Raymond Kennington <raymondk@one.net.au>';
SubjectEdit: TEdit
  Text := 'VenStat: New Update File';
AttachEdit: TEdit
  Text := 'C:\AUTOEXEC.BAT';

and pretend that there is a function

function ExtractAddress(FullAddress: String): String;

that takes an address of the form
  'Raymond Kennington <raymondk@one.net.au>'
and returns
  'raymond@one.net.au'

procedure TForm1.SendMailButton(Sender: TObject);
begin
  SmtpClient.Host := 'mail.one.net.au';
  SmtpClient.Port := 'smtp';
  SmtpClient.Connect;
  { Disable the button so the user cannot send another mail
    just yet. }
  TButton(Sender).Enabled := False;
end;

procedure TForm1.SmtpClientRequestDone(Sender: TObject; RqType:
TSmtpRequest; Error: Word);
begin
  case RqType of
    smtpConnect:
      begin
        TSmtpCli(Sender).SignOn := LocalHostName;
        TSmtpCli(Sender).Helo;
      end;
    smtpHelo:
      begin
        TSmtpCli(Sender).FromName := ExtractAddress(EditFrom.Text);
        TSmtpCli(Sender).MailFrom;
      end;
    smtpMailFrom:
      begin
        TSmtpCli(Sender).RcptName.Clear;
        TSmtpCli(Sender).RcptName.Add(ExtractAddress(ToEdit.Text));
        TSmtpCli(Sender).RcptTo;
      end;
    smtpRcptTo:
      begin
        TSmtpCli(Sender).HdrFrom := FromEdit.Text;
        TSmtpCli(Sender).HdrTo := ToEdit.Text;
        TSmtpCli(Sender).HdrSubject := SubjectEdit.Text
        TSmtpCli(Sender).EmailFiles.Clear;
        TSmtpCli(Sender).EmailFiles.Add(AttachEdit.Text);
        TSmtpCli(Sender).Data;
      end;
    smtpData:
      begin
        TSmtpCli(Sender).Quit;
      end;
    smtpQuit:
      begin
        SendMailButton.Enabled := True;
      end;
  end;
end;

Steve 'Sly' Williams
IT Development
Cable & Wireless Optus Outsourcing Services
swilliams@fm.optus.net.au
------------------------------------------------------------
"Programming is an art form that fights back." -Unknown
------------------------------------------------------------

> Message added to the Memo:
> 
> < 220 hardblue.one.net.au ESMTP
> 
> Raymond
> 
> Stephen Williams wrote:
> > 
> > Second, are you using TSmtpClient or TSmtpCli?  TSmtpClient 
> was replaced a
> > long time ago by TSmtpCli.
> 
> I am using TSMTPCli, but I named the component -SMTPClient-.
> 
> Raymond