|
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;
|
Latest Comments View All Add New RSS ATOM