[twsocket] How to send multiple email with SMTPClient

Dennis Poon pmh@netvigator.com
Thu, 1 Jun 2000 01:36:14 +0800


I am using the SMTPClient component to send email and I wrote the following
unit to encapsulate a function
procedure SendICSEmail(SMTPServer : string; Tos : array of string; From,
Subject, Body: string);

to do that.

I am wonder if there is a better way to send multiple emails (I currently
keep on Applicatin.processMessages until the SMTPClient is ready again
before I send a second email).  However, if the machine is no online, or
some other reasons, it will take forever to become ready so I put a check in
time to abort it.

Is there a property to check within SMTPClient to see if it has already
failed to connect or send.  that way, I don't have to wait until it is too
long.

Thanks.

Dennis
-----
unit ICSSendMail;
interface
uses SMTPProt;
procedure SendICSEmail(SMTPServer : string; Tos : array of string; From,
Subject, Body: string);

implementation
uses SysUtils,Forms;
var
  SMTPClient : TSMTPCli;

procedure SendICSEmail(SMTPServer : string; Tos : array of string; From,
Subject, Body: string);
var i : integer;
    StartTime : TDateTime;
begin
    SmtpClient.Host := SMTPServer;
    SmtpClient.Port := 'smtp';
    if Not SMTPClient.Connected then begin
       SmtpClient.Connect;
       StartTime := now();
       while Not SmtpClient.Connected do begin
         Application.ProcessMessages;
         if (Now()-StartTime) > 0.1/24/60 then begin
            raise Exception.Create('SMTP Connection fails within reasonable
time');
         end;
       end;

       StartTime := now();
       while Not (SmtpClient.State in [smtpReady]) do begin
         Application.ProcessMessages;
         if (Now()-StartTime) > 0.1/24/60 then begin
            raise Exception.Create('SMTP not ready within reasonable time');
         end;
       end;
       SMTPClient.Helo;
    end;

    StartTime := now();
    while Not (SmtpClient.State in [smtpReady]) do begin
      Application.ProcessMessages;
      if (Now()-StartTime) > 0.5/24/60 then begin
         raise Exception.Create('SMTP not ready within reasonable time');
      end;
    end;


    SmtpClient.RcptName.Clear;
    for i := low(Tos) to High(Tos) do begin
      SmtpClient.RcptName.Add(Tos[i]);
    end;

    SmtpClient.HdrFrom         := From;
    SmtpClient.HdrTo           := '';
    SmtpClient.HdrSubject      := Subject;
    SmtpClient.SignOn          := 'Signature';
    SmtpClient.FromName        := From;
    SmtpClient.MailMessage.Clear;
    SmtpClient.MailMessage.Text := Body;
//    SmtpClient.EmailFiles      := FileAttachMemo.Lines;
    SmtpClient.Mail;
end;

initialization

  SMTPClient := TSMTPCli.Create(nil);

finalization

  SMTPClient.Free;

end.