Shiv Kumar
 Hobbyist Filmmaker / Editor

Sending an Email from an ISAPI application (Threaded)

Not Rated YetNot Rated YetNot Rated YetNot Rated YetNot Rated Yet0votes
January 13, 2008 11:24 AM  Views: 1620   Favorited: 0 Favorite It Comments: 0
Filed Under:  Programming
Tags:  Database, Delphi, ISAPI
 
The process of sending an email in Delphi is very simple. One can use any STMP components for this purpose. In this example, we'll use the Indy (Winshoes) SMTP component but one can easily alter the execute method to be used with any SMTP component.

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.

exclamation.gif If you plan to send bulk emails from your ISAPI, I reccomend you build a separate ISAPI Application or WebService to do this job specifically. Take a look at the Tutorial Using WebServices from a Delphi 5/6 ISAPI for an example of how to do this. This is the preferred method in my opinion.
To keep things manageable/simple, we'll create our thread class in a separate unit. The code in this unit will look like the following:
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.
Now all we need to do is include this unit is the uses clause of our ISAPI/CGI application and create the thread when required. Notice that the TEmailThread has properties similar to that of an SMTP component. This makes the process of sending an email using a thread very simple.

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;
This code snippet is straight out of, one of the ISAPI applications that drives my web site. If you've signed my guest book, you will notice that the automatic email you received soon after you hit the submit button was produced by this code. The thread is created here, but is freed in the thread's Execute method. Notice, that in the OnCreate method of the thread, we've set FreeOnTerminate := True and then in the last line of the Execute method, we terminate the thread. You can substitute any SMTP component of your choice in this code.

Comments have been Disabled for this post





Leave A Comment

Shiv Kumar
Gainesville, Virginia,
United States
Member Bio Member Skills/Specialization

Bio

close

Specializations

close
Photographer
Landscape
Nature
Portrait
Videographer/Cinematographer
Interview
Landscape
Nature
Portrait
 
Privacy Policy | Terms Of Service | Contact Us | Support | Help/FAQ | News