|
|
Sending and Receiving MailProbably the most common operation you do on the Internet is to send and receive e-mail. There is generally little need to write a complete application to handle e-mail, because some of the existing programs are rather complete. For this reason, I have no intention of writing a general-purpose mail program here. You can find some examples of those among Indy demos. Other than creating a general-purpose mail application, you can do many things with the mail components and protocols; I've grouped the possibilities into two areas:
Mail In and OutUsing the mail protocols with Indy means placing a message component (IdMessage) in your application, filling it with data, and then using the IdSMTP component to send the mail message. To retrieve a mail message from your mailbox, use the IdPop3 component, which will return an IdMessage object. To give you an idea how this process works, I've written a program for sending mail to multiple people at once, using a list stored in an ASCII file. I originally used this program to send mail to people who sign up on my website, but later I extended the program by adding database support and the ability to read subscriber logs automatically. The original version of the program is still a good introduction to the use of the Indy SMTP component. The SendList program keeps a list of names and e-mail addresses in a local file, which is displayed in a list box. A few buttons allow you to add and remove items, or to modify an item by removing it, editing it, and then adding it again. When the program closes, the updated list is automatically saved. Now let's get to the interesting portion of the program. The top panel, shown in Figure 19.3 at design time, allows you to enter the subject, the sender address, and the information used to connect to the mail server (hostname, username, and eventually a password). You'll probably want to make the value of these edit boxes persistent, possibly in an INI file. I haven't done this, only because I don't want you to see my mail connection details! The value of these edit boxes, along with the list of addresses, allows you to send the series of mail messages (after customizing them) with the following code:
procedure TMainForm.BtnSendAllClick(Sender: TObject);
var
nItem: Integer;
Res: Word;
begin
Res := MessageDlg ('Start sending from item ' +
IntToStr (ListAddr.ItemIndex) + ' (' +
ListAddr.Items [ListAddr.ItemIndex] + ')?'#13 +
'(No starts from 0)', mtConfirmation, [mbYes, mbNo, mbCancel], 0);
if Res = mrCancel then
Exit;
if Res = mrYes then
nItem := ListAddr.ItemIndex
else
nItem := 0;
// connect
Mail.Host := eServer.Text;
Mail.UserName := eUserName.Text;
if ePassword.Text <> '' then
begin
Mail.Password := ePassword.Text;
Mail.AuthenticationType := atLogin;
end;
Mail.Connect;
// send the messages, one by one, prepending a custom message
try
// set the fixed part of the header
MailMessage.From.Name := eFrom.Text;
MailMessage.Subject := eSubject.Text;
MailMessage.Body.SetText (reMessageText.Lines.GetText);
MailMessage.Body.Insert (0, 'Hello');
while nItem < ListAddr.Items.Count do
begin
// show the current selection
Application.ProcessMessages;
ListAddr.ItemIndex := nItem;
MailMessage.Body [0] := 'Hello ' + ListAddr.Items [nItem];
MailMessage.Recipients.EMailAddresses := ListAddr.Items [nItem];
Mail.Send(MailMessage);
Inc (nItem);
end;
finally // we're done
Mail.Disconnect;
end;
end;
Another interesting example of using mail is to notify developers of problems within applications (a technique you might want to use in an internal application rather than in one you'll distribute widely). You can obtain this effect by modifying the ErrorLog example from Chapter 2 and sending mail when an exception (or one of a given type only) occurs. |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Copyright © 2004-2025 "Delphi Sources" by BrokenByte Software. Delphi Programming Guide |