It is not advisable to send an Email directly from within your ISAPI/CGI application as this may slow down the ISAPI application while it waits for the SMTP component to connect with your SMTP server. What we'll do instead is create a thread and send the email from within the thread. This will allow the ISAPI application to continue on without having to wait for the email to be sent.
unit SndMailThrd; interface uses Windows, Messages, SysUtils, Classes, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP, IdMessage, IdEMailAddress; type TEmailThread = class(TThread) private FSubject: string; FReplyTo: string; FHost: string; FFromAddress: string; FUserID: string; FToAddress: TStrings; FBody: TStrings; FFromName: string; procedure SetBody(const Value: TStrings); procedure SetToAddress(const Value: TStrings); { Private declarations } protected procedure OnThreadTerminate(Sender : TObject); public procedure Execute; override; constructor Create(CreateSuspended : Boolean; ThreadPriority: TThreadPriority); destructor Destroy;override; property Host : string read FHost write FHost; property UserID : string read FUserID write FUserID; property Body : TStrings read FBody write SetBody; property ToAddress : TStrings read FToAddress write SetToAddress; property Subject : string read FSubject write FSubject; property FromAddress : string read FFromAddress write FFromAddress; property FromName : string read FFromName write FFromName; property ReplyTo : string read FReplyTo write FReplyTo; end; implementation { Example of usage } {with TEmailThread.Create(True, tpHigher) do begin Host := 'smtp-server'; UserID := 'skumar'; ToAddress.Add('shiv@matlus.com'); Subject := 'Threaded Email'; FromAddress := 'shiv@matlus.com'; ReplyTo := 'shiv@matlus.com'; Body.Add('This is a test email !!'); Resume; end;} { TEmailThread } constructor TEmailThread.Create(CreateSuspended: Boolean; ThreadPriority: TThreadPriority); begin Priority := ThreadPriority; FToAddress := TStringList.Create; FBody := TStringList.Create; inherited Create(CreateSuspended); FreeOnTerminate := True; end; destructor TEmailThread.Destroy; begin FToAddress.Free; FBody.Free; inherited; end; procedure TEmailThread.SetBody(const Value: TStrings); begin FBody.Assign(Value); end; procedure TEmailThread.SetToAddress(const Value: TStrings); begin FToAddress.Assign(Value); end; procedure TEmailThread.Execute; var IdMsg : TIdMessage; begin IdMsg := TIdMessage.Create(nil); try with TIdSMTP.Create(nil) do begin UserID := FUserID; Host := FHost; IdMsg.Body.Assign(Body); IdMsg.Recipients.EMailAddresses := ToAddress.Text; IdMsg.Subject := Subject; IdMsg.From.Address := FromAddress; IdMsg.From.Name := FromName; IdMsg.ReplyTo.Address := ReplyTo; Connect; Send(IdMsg); Disconnect; Free; end; finally FreeAndNil(IdMsg); Terminate; end; end; procedure TEmailThread.OnThreadTerminate(Sender: TObject); begin end; end.
Lets look at an example of how we can use this thread class. In a function or procedure in your ISAPI application in which you want to send an email, you need a have the following lines of code.
with TEMailThread.Create(True,tpHigher) do begin FromName := 'Shiv Kumar'; Subject := 'Thankyou for the Visit on ' FormatDateTime('mmmm dd yyyy at hh:nn:ss', Now); ReplyTo := 'shiv@matlus.com'; Host := 'smtp-server'; UserID := 'shivk'; FromAddress := 'shiv@matlus.com'; ToAddress.Add(sEMail); Body.Add(Format('Dear %s %s,' #13#10 'Thank you for taking the time to fill my guest book. I hope you enjoyed what you saw on my web site.' ' I am constantly making changes to my site, so please come back often.' #13#10 'I take special care of the Tutorials section of my web site. I hope you found them useful and encouraging.' #13#10 #13#10 'SITE INFORMATION:' #13#10 'Web Site: http://www.matlus.com' #13#10 'Web Server: Windows 2000 Advanced Server' #13#10 'Database: Interbase 6.0' #13#10 'Generators: Dynamically generated by a number of Delphi built ISAPI DLLs' #13#10 ' If you need more information on the tools I''ve used to develop this site,' ' please feel free to contact me.' #13#10 'Regards,' #13#10 'Shiv.',[sFirstName,sLastName])); Resume; end;