A Sip From The Firehose: Wednesday, October 28, 1998

By: David Intersimone

Abstract: EMail Blasting with Borland Delphi 4.

Wednesday, October 28, 1998
Scotts Valley, California

EMail Blasting with Borland Delphi 4.

If you attended our conference this year in Denver, you probably received at least one email blast from me. The email message was sent by an application that uses Delphi 4's SMTP component (check out the Internet palette page). The Email Blaster Souffle application becomes the third addition to my "Betty Hacker's Down Home Developer's Cookbook" (the first recipe was Fresh Blackberry Internet Browser, the second recipe was Red Hot Salsa Drawings with Autocad R14). You can find an index of other souffle recipes on the web.

I do not like spam, neither Hormel SPAM nor the email kind. When I use my email blaster it is only to friends, family, partners and conference attendees who want to receive the messages. The Inprise listservers only send email messages to those who sign up for specific products. We should all do everything we can to Fight Spam on the Internet.

To add to your own cookbook just cut and paste the recipe included below. Zip files containing the source code, form files, and sample database table are available for Delphi and C++Builder versions.


Betty Hacker's Quick 'n Easy
Email Blaster Souffle
Delphi 4

To create the best Email Blaster Souffle you need to start with the choicest ingredients. With Borland Delphi 4 and the SMTP component you can create your own email blaster program.

Ingredients:
3 pounds Delphi 3 (Professional or Client Server Suite strength)
1 blank Delphi form
1 data module
1 Table Component
1 DataSource Component
10 helpings sample Delphi source code
1/2 cup powered or confectioners sugar
2 tsp baking powder
4 egg whites
3 tablespoons butter
1/2 cup milk
Flavoring of your choice - chocolate, orange, maple, etc

Preparation:
Preheat your PC to 110 volts (220 volts for most developers outside the US).

To your hard drive bowl...
      Mix in Delphi 4

Combine ingredients and bake the Email Blaster Souffle application.

Souffle

Sift and combine the ingredients for the Email Blaster Souffle: Select File | New Application.

Roll out the form's caption: Set the Form's caption property to Email Blaster or your own caption text to taste.

Fold in a Data Module: Select File | New... and chose a Data Module from the new items page.

Measure in a Data Source: Add a Table and Datasource component to the Data Module. Set the DatabaseName to an alias that points to your database that contains email addresses (I used the alias - Email). Set the TableName property to the email address table (my Paradox table is named Email.db).

Chop in an SMTP component: From the Delphi 4 Internet component palette, drop the NMSMTP component on to your main form. This is the component that will let you send mail via a pop mail server.

Stir in a Menu component: From the Standard component palette, drop down a Menu component. Plug in the captions for several menu items including: File, Exit, MailServer, Database, Blast, and Help. See form for other menu items.

Pour in a StatusBar component: From the Win32 component palette, drop down a StatusBar component. Set the SimplePanel property to True. This lets you use ths status bar as a place to display text messages. Use code in event handlers like the following to display status information: StatusBar1.SimpleText := 'Packet Sent';

Blend in event handlers for Menu and SMTP components: Event handlers for the SMTP component let you keep track of the status of server connections and message send status. The event handlers for the Menu component let you connect to the PopMail server, open the database, start and stop the email blast.

The StartBlast menu item event handler is where all the processing is done. The code is as follows.

procedure TBlastForm.Start1Click(Sender: TObject);
begin
  Start1.Enabled := false;
  Stop1.Enabled := true;
  BlastLabel.Caption := 'Blasting!';
  Blasting := true;
  while (not DM.EmailTable.Eof) and Blasting do begin
    SendIt;
    // wait for completion of sendmail
    while SendingMail and Blasting do 
    // let the application process messages while we wait     
    Application.ProcessMessages;  
    DM.EmailTable.Next;
    FillMemo;
    // refreshes the contents of the memo component
    Application.ProcessMessages; 
  end;
  Blasting := false;
  Start1.Enabled := true;
  Stop1.Enabled := false;
  BlastLabel.Caption := 'No Blast';
end;

The OnCreate event handler for the form looks for a text file, Message.txt, that contains the message to send. OnCreate also opens a text file that will be used as a log of what happened during the blaster session.

procedure TBlastForm.FormCreate(Sender: TObject);
var
  I : integer;
begin
  SendingMail := false;
  // open message.txt file and load into a memo field
  MessageText := TStringList.Create;
  MessageText.LoadFromFile('message.txt');
  // open log file for output
  AssignFile(LogFile,FormatDateTime('yymmdd',Date)+'.txt');
  Rewrite(LogFile);
  Writeln(LogFile, '[ Start of Blast Log File ]');
  Writeln(LogFile);
  Writeln(LogFile, '< Blast Message Text >');
  Writeln(LogFile);
  for I := 0 to MessageText.Count - 1 do
    Writeln(LogFile,MessageText.Strings[I]);
  Writeln(LogFile);
  Writeln(LogFile, '< Log of Email Messages Sent >');
  Writeln(LogFile);
end;

FillMemo is a method I use to create the body of the message to be send. You can use this method to add to the message at runtime. In this example I only add today's date and a personal greeting to the text from the Message.txt file.

procedure TBlastForm.FillMemo;
begin
  MessageMemo.Lines.Clear;
  MessageMemo.Lines.Add(FormatDateTime('mmmm dd, yyyy',Now));
  MessageMemo.Lines.Add('Hello '+DM.EmailTableFIRSTNAME.Value+':');
  MessageMemo.Lines.AddStrings(MessageText);
end;

SendIt is the method where I use the SMTP component to send the message via the Mail Server. Notice that I check to see if I have a valid email address (one that contains an @ sign in the text). My main form contains text fields where the subject for the message (MessageSubject.Text), the sender's email address(FromEmailAddress.Text), and the sender's name (MesageFrom.Text).

procedure TBlastForm.SendIt;
begin
  if Pos('@',DM.EmailTableEMail.Value) > 0 then begin
    SendingMail := true;
    NMSMTP1.ClearParameters;
    NMSMTP1.PostMessage.Body.Clear;
    // text string of senders email address
    NMSMTP1.PostMessage.FromAddress := FromEmailAddress.Text;  
    NMSMTP1.PostMessage.FromName := MessageFrom.Text;  // name of sender
    // email address from database
    NMSMTP1.PostMessage.ToAddress.Add(DM.EmailTableEMail.Value); 
    NMSMTP1.PostMessage.Subject := MessageSubject.Text;  // subject text
    // lines for the message body
    NMSMTP1.PostMessage.Body.AddStrings(MessageMemo.Lines); 
    NMSMTP1.PostMessage.LocalProgram := 'EmailBlast';
    NMSMTP1.SendMail;
    Writeln(LogFile, '+ message sent: '
    +DM.EmailTableFIRSTNAME.Value+' '+DM.EmailTableLASTNAME.Value+'  '
    +DM.EmailTableEMAIL.Value)
  end
  else
    Writeln(LogFile, '- invalid/no email address: '
    +DM.EmailTableFIRSTNAME.Value+' '+DM.EmailTableLASTNAME.Value+' '
    +DM.EmailTableEMAIL.Value);
end;

Bake for approximately 3-5 seconds or until the Souffle has risen and the top is golden brown.

Bake (hit the F9 key) for approximately 3-5 seconds or until the Souffle has risen and the top is golder brown. Serve with the sauce and beverage of your choice. When the Email Blaster Souffle is running, type in the IP address of your Pop Mail Server, Chose the MailServer | Connect menu item, and chose the Database | Open Database menu item. You are now ready to start mail blasting. You can also use the DBNavigator to edit, insert, and delete email database entries. To start the email blast, choose the Blast Start menu item. If you follow the instructions carefully you should see the following result on your screen.

Yield:
One Email Blaster Souffle application per developer.

Variations:
You can do the same thing with C++Builder 3.

David Intersimone
davidi@inprise.com


Server Response from: ETNASC02