[ Pobierz całość w formacie PDF ]
.An alternative is to use an existingserver application, such as a mail server, and write the two specialized programs based onthe mail protocols.The data sent over this connection will generally be formatted in spe-cial ways, so you ll want to use a specific e-mail addresses for these messages (not your pri-mary e-mail address).As an example, you could rewrite the earlier DbSock example todispatch mail messages instead of using a custom socket connection.This will give you theadvantage of being firewall-friendly and allowing the server to be temporarily offline, asthe requests would be kept on the mail server.Sending Messages with Your Mail ProgramThe simplest technique for automating the generation of an e-mail message is to use yourexisting mail application, adding a message to its outbox.Using the ShellExecute API func-tion, you can easily send a message to the default mail program registered on the computer.Copyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com 2874c21.qxd 7/2/01 2:36 PM Page 933Sending and Receiving Mail 933To test this technique, I ve prepared a simple form with two edit boxes and a memo for theinput.Pressing a button creates a string with all the information about the message and thensends it, simply executing the string with the mailto: prefix.Here is the code of the Send but-ton of the MailGen example from the companion CD:usesusesShellApi;procedure TForm1.BtnSendClick(Sender: TObject);procedurevarvarstrMsg: string;I: Integer;beginbegin// set the basic informationstrMsg :=  mailto: + EditAddress.Text +  ?Subject= + EditSubject.Text + &Body= ;// add first lineif Memo1.Lines.Count > 0 thenifthenstrMsg := strMsg + Memo1.Lines [0];// add subsequent lines separated by the newline symbolfor I := 1 to Memo1.Lines.Count - 1 dofortodostrMsg := strMsg +  %0D%0A + Memo1.Lines [I];// send the messageShellExecute (Handle,  open , pChar (strMsg),   ,   , SW_SHOW);end;endTo show the body of the message on multiple lines, you can separate each line with thecarriage return and line feed characters (usually indicated in Delphi as #13 and #10).Thesevalues should be explicitly added to the string in hexadecimal format and prefixed by the %sign, as required by a URL.You can actually obtain this encoding automatically by using theNMURL component.NOTEYou can also send mail with the TSendMail predefined action, which is based on the MAPIstandard.Mail In and OutTo showcase the development of simple e-mail management programs, I could build an exampleof how you can send and receive mail.The Indy components, though, include a rather completeset of examples, and I don t see any reason to duplicate those, as using the mail protocols meansplacing a message component (IdMessage) in your application, filling it with data, and thenusing the IdSMTP component to send the mail message.To retrieve a mail message from yourmailbox, use the IdPop3 component, which will return you an IdMessage object.Copyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com 2874c21.qxd 7/2/01 2:36 PM Page 934934 Chapter 21 " Internet Programming: Sockets and Indy ComponentsJust to give you an idea of how this works, I ve written a program for sending mail to mul-tiple people at once, using a list stored in an ASCII file.I originally used this program myselffor sending mail to people who sign up on my Web site, but later I extended the program byadding database support and reading subscriber logs automatically.The original version ofthe program is still a good introduction to the use of the SMTP component of Indy.The SendList program keeps a list of names and e-mail addresses in a local file, which isdisplayed in a list box.A few buttons allow you to add and remove items, or modify them byremoving the item, editing it, and then adding the item again.When the program closes, theupdated list is automatically saved.Now let s get to the interesting portion of the program.The top-most panel, shown in Figure 21.4, allows you to enter the subject, the senderaddress, and the information used to connect to the mail server (hostname, username, andeventually a password).FI GURE 21.4:The SendList program inactionYou ll probably want to make the value of these edit boxes persistent, possibly in an INIfile.I haven t done this, only because I don t really want you to see my mail connectionCopyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com 2874c21 [ Pobierz caÅ‚ość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • wpserwis.htw.pl