Delphi Programming Guide
Delphi Programmer 

Menu  Table of contents
Bookmark and Share

Part I - Foundations
  Chapter 1 Ц Delphi 7 and Its IDE
  Chapter 2 Ц The Delphi Programming Language
  Chapter 3 Ц The Run-Time Library
  Chapter 4 Ц Core Library classes
  Chapter 5 Ц Visual Controls
  Chapter 6 Ц Building the User Interface
  Chapter 7 Ц Working with Forms
Part II - Delphi Object-Oriented Architectures
  Chapter 8 Ц The Architecture of Delphi Applications
  Chapter 9 Ц Writing Delphi Components
  Chapter 10 Ц Libraries and Packages
  Chapter 11 Ц Modeling and OOP Programming (with ModelMaker)
  Chapter 12 Ц From COM to COM+
Part III - Delphi Database-Oriented Architectures
  Chapter 13 Ц Delphi's Database Architecture
  Chapter 14 Ц Client/Server with dbExpress
  Chapter 15 Ц Working with ADO
  Chapter 16 Ц Multitier DataSnap Applications
  Chapter 17 Ц Writing Database Components
  Chapter 18 Ц Reporting with Rave
Part IV - Delphi, the Internet, and a .NET Preview
  Chapter 19 Ц Internet Programming: Sockets and Indy
  Chapter 20 Ц Web Programming with WebBroker and WebSnap
  Chapter 21 Ц Web Programming with IntraWeb
  Chapter 22 Ц Using XML Technologies
  Chapter 23 Ц Web Services and SOAP
  Chapter 24 Ц The Microsoft .NET Architecture from the Delphi Perspective
  Chapter 25 Ц Delphi for .NET Preview: The Language and the RTL
  Appendix A Ц Extra Delphi Tools by the Author
  Appendix B Ц Extra Delphi Tools from Other Sources
  Appendix C Ц Free Companion Books on Delphi
  List of Figures    
  List of tables    
  List of Listings    
  List of Sidebars  

Previous Section Next Section

Sending and Receiving Mail

Probably 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:

Automatic Generation of Mail Messages  An application you've written can have an About box for sending a registration message back to your marketing department or a specific menu item for sending a request to your tech support. You might even decide to enable a tech-support connection whenever an exception occurs. Another related task could automate the dispatching of a message to a list of people or generate an automatic message from your website (an example I'll show you toward the end of this chapter).

Use of Mail Protocols for Communication with Users Who Are Only Occasionally Online  When you must move data between users who are not always online, you can write an application on a server to synchronize among them, and you can give each user a specialized client application for interacting with the server. An alternative is to use an existing server application, such as a mail server, and write the two specialized programs based on the mail protocols. The data sent over this connection will generally be formatted in special ways, so you'll want to use a specific e-mail address for these messages (not your primary e-mail address). As an example, you could rewrite the earlier IndyDbSock example to dispatch mail messages instead of using a custom socket connection. This approach has the advantage of being firewall-friendly and allowing the server to be temporarily offline, because the requests will be kept on the mail server.

Mail In and Out

Using 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).

Click To expand
Figure 19.3:  The SendList pro-gram at design time

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);
  nItem: Integer;
  Res: Word;
  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
  if Res = mrYes then
    nItem := ListAddr.ItemIndex
    nItem := 0;
  // connect
  Mail.Host := eServer.Text;
  Mail.UserName := eUserName.Text;
  if ePassword.Text <> '' then
    Mail.Password := ePassword.Text;
    Mail.AuthenticationType := atLogin;
  // send the messages, one by one, prepending a custom message
    // 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
      // show the current selection
      ListAddr.ItemIndex := nItem;
      MailMessage.Body [0] := 'Hello ' + ListAddr.Items [nItem];
      MailMessage.Recipients.EMailAddresses := ListAddr.Items [nItem];
      Inc (nItem);
  finally // we're done

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.

Previous Section Next Section



Clever Internet Suite

»нтернет-компоненты Clever Internet Suite Ч набор компонентов дл€ Delphi/C++Builder, позвол€ющих реализовать обмен данными по сети в вашем приложении. — помощью Clever Internet Suite вы можете реализовать различные функции дл€ работы в сети интернет: скачивание файлов, отправка данных, введенных пользователем, рассылка и получение почты, использовать клиенты HTTP, FTP, SMTP, POP3, IMAP и NNTP; серверы SMTP, POP3, IMAP и FTP и многое другое.

Copyright © 2004-2019 "Delphi Sources". Delphi Programming Guide
√руппа ¬ онтакте   Facebook   —сылка на Twitter