Building a Mailing List with Borland Delphi - Status reports
(Page 5 of 5 )
There are two types of status reports being displayed when the program is running. The one with the colorful text is created by the code. It actually just indicates what is happening in terms of how many messages are being sent or how many messages are left to be sent. The second type of status report is done automatically by the idsmtp component. On the screen shot, take a look at the memo component just below the "send mail" button to see what I'm talking about. Let's add the code that makes this possible. Double click on the idsmtp's OnStatus event and add the following code:
procedure TForm1.IdSMTPStatus(ASender: TObject; const AStatus: TIdStatus;
const AStatusText: String);
begin
memo2.Lines.Add(AStatustext);
end;
The Send to All option
This option requires nowhere near the amount of code that we needed for the other option. It is relatively straightforward. All we do here is retrieve ALL the email addresses in the database:
if rb2.Checked then begin
query1.Close;
query1.sql.Text:='SELECT * FROM contacts ';
query1.Open;
Then we run a "while" loop to retrieve the email addresses one-by-one and then send the message off, while at the same time giving a visual report of what is happening:
while not query1.Eof do begin
re.Perform(EM_SETSEL, -1, -1);
re.SelAttributes.Color:= clGreen;
re.SelAttributes.Style:=[fsBold];
re.SelText:='Sending message#: ' +inttostr(query1.RecNo)+ ' of
' ;
re.Perform(EM_SETSEL, -1, -1);
re.SelAttributes.Color:= clBlack;
re.SelText:=inttostr(query1.RecordCount)+#13#10;
idmessage.Recipients.EMailAddresses:=
query1.fieldbyname('email').Text;
try
idSMTP.send(idmessage);
except
on E: EIdSMTPReplyError do
begin
ShowMessage(E.Message);
Exit;
end;
end;
query1.Next;
end;
When all the messages have been sent, a message is displayed and the connection is discontinued:
re.Perform(EM_SETSEL, -1, -1);
re.SelAttributes.Color:= clRed;
re.SelAttributes.Style:=[fsBold];
re.SelText:='All messages have been successfully sent.';
if idsmtp.Connected then idsmtp.Disconnect;
end;
Error reporting
In both options, I've put the sending of the email messages in a try...except block, so that if there is an error, it will be displayed in a dialog box and the sending of the email messages will be stopped:
try
idSMTP.send(idmessage);
except
on E: EIdSMTPReplyError do
begin
ShowMessage(E.Message);
Exit;
end;
In the next part we will look at how to manage our contact details.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |