Main.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 108528: Main.pas
  11. {
  12. { Rev 1.0 14/08/2004 12:29:18 ANeillans
  13. { Initial Checkin
  14. }
  15. {
  16. Demo Name: SMTP Server
  17. Created By: Andy Neillans
  18. On: 27/10/2002
  19. Notes:
  20. Demonstration of SMTPServer (by use of comments only!!)
  21. Read the RFC to understand how to store and manage server data, and
  22. therefore be able to use this component effectivly.
  23. Version History:
  24. 14th Aug 04: Andy Neillans
  25. Updated for Indy 10, rewritten IdSMTPServer
  26. 12th Sept 03: Andy Neillans
  27. Cleanup. Added some basic syntax checking for example.
  28. Tested:
  29. Indy 10:
  30. D5: Untested
  31. D6: Untested
  32. D7: Untested
  33. }
  34. unit Main;
  35. interface
  36. uses
  37. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  38. IdBaseComponent, IdComponent, IdTCPServer, IdSMTPServer, StdCtrls,
  39. IdMessage, IdEMailAddress, IdCmdTCPServer, IdExplicitTLSClientServerBase;
  40. type
  41. TForm1 = class(TForm)
  42. Memo1: TMemo;
  43. Label1: TLabel;
  44. Label2: TLabel;
  45. Label3: TLabel;
  46. ToLabel: TLabel;
  47. FromLabel: TLabel;
  48. SubjectLabel: TLabel;
  49. IdSMTPServer1: TIdSMTPServer;
  50. btnServerOn: TButton;
  51. btnServerOff: TButton;
  52. procedure btnServerOnClick(Sender: TObject);
  53. procedure btnServerOffClick(Sender: TObject);
  54. procedure IdSMTPServer1MsgReceive(ASender: TIdSMTPServerContext;
  55. AMsg: TStream; var LAction: TIdDataReply);
  56. procedure IdSMTPServer1RcptTo(ASender: TIdSMTPServerContext;
  57. const AAddress: String; var VAction: TIdRCPToReply;
  58. var VForward: String);
  59. procedure IdSMTPServer1UserLogin(ASender: TIdSMTPServerContext;
  60. const AUsername, APassword: String; var VAuthenticated: Boolean);
  61. procedure IdSMTPServer1MailFrom(ASender: TIdSMTPServerContext;
  62. const AAddress: String; var VAction: TIdMailFromReply);
  63. procedure IdSMTPServer1Received(ASender: TIdSMTPServerContext;
  64. AReceived: String);
  65. private
  66. { Private declarations }
  67. public
  68. { Public declarations }
  69. end;
  70. var
  71. Form1: TForm1;
  72. implementation
  73. {$R *.DFM}
  74. procedure TForm1.btnServerOnClick(Sender: TObject);
  75. begin
  76. btnServerOn.Enabled := False;
  77. btnServerOff.Enabled := True;
  78. IdSMTPServer1.active := true;
  79. end;
  80. procedure TForm1.btnServerOffClick(Sender: TObject);
  81. begin
  82. btnServerOn.Enabled := True;
  83. btnServerOff.Enabled := False;
  84. IdSMTPServer1.active := false;
  85. end;
  86. procedure TForm1.IdSMTPServer1MsgReceive(ASender: TIdSMTPServerContext;
  87. AMsg: TStream; var LAction: TIdDataReply);
  88. var
  89. LMsg : TIdMessage;
  90. LStream : TFileStream;
  91. begin
  92. // When a message is received by the server, this event fires.
  93. // The message data is made available in the AMsg : TStream.
  94. // In this example, we will save it to a temporary file, and the load it using
  95. // IdMessage and parse some header elements.
  96. LStream := TFileStream.Create(ExtractFilePath(Application.exename) + 'test.eml', fmCreate);
  97. Try
  98. LStream.CopyFrom(AMsg, 0);
  99. Finally
  100. FreeAndNil(LStream);
  101. End;
  102. LMsg := TIdMessage.Create;
  103. Try
  104. LMsg.LoadFromFile(ExtractFilePath(Application.exename) + 'test.eml', False);
  105. ToLabel.Caption := LMsg.Recipients.EMailAddresses;
  106. FromLabel.Caption := LMsg.From.Text;
  107. SubjectLabel.Caption := LMsg.Subject;
  108. Memo1.Lines := LMsg.Body;
  109. Finally
  110. FreeAndNil(LMsg);
  111. End;
  112. end;
  113. procedure TForm1.IdSMTPServer1RcptTo(ASender: TIdSMTPServerContext;
  114. const AAddress: String; var VAction: TIdRCPToReply;
  115. var VForward: String);
  116. begin
  117. // Here we are testing the RCPT TO lines sent to the server.
  118. // These commands denote where the e-mail should be sent.
  119. // RCPT To address comes in via AAddress. VAction sets the return action to the server.
  120. // Here, you would normally do:
  121. // Check if the user has relay rights, if the e-mail address is not local
  122. // If the e-mail domain is local, does the address exist?
  123. // The following actions can be returned to the server:
  124. {
  125. rAddressOk, //address is okay
  126. rRelayDenied, //we do not relay for third-parties
  127. rInvalid, //invalid address
  128. rWillForward, //not local - we will forward
  129. rNoForward, //not local - will not forward - please use
  130. rTooManyAddresses, //too many addresses
  131. rDisabledPerm, //disabled permentantly - not accepting E-Mail
  132. rDisabledTemp //disabled temporarily - not accepting E-Mail
  133. }
  134. // For now, we will just always allow the rcpt address.
  135. VAction := rAddressOk;
  136. end;
  137. procedure TForm1.IdSMTPServer1UserLogin(ASender: TIdSMTPServerContext;
  138. const AUsername, APassword: String; var VAuthenticated: Boolean);
  139. begin
  140. // This event is fired if a user attempts to login to the server
  141. // Normally used to grant relay access to specific users etc.
  142. VAuthenticated := True;
  143. end;
  144. procedure TForm1.IdSMTPServer1MailFrom(ASender: TIdSMTPServerContext;
  145. const AAddress: String; var VAction: TIdMailFromReply);
  146. begin
  147. // Here we are testing the MAIL FROM line sent to the server.
  148. // MAIL FROM address comes in via AAddress. VAction sets the return action to the server.
  149. // The following actions can be returned to the server:
  150. { mAccept, mReject }
  151. // For now, we will just always allow the mail from address.
  152. VAction := mAccept;
  153. end;
  154. procedure TForm1.IdSMTPServer1Received(ASender: TIdSMTPServerContext;
  155. AReceived: String);
  156. begin
  157. // This is a new event in the rewrite of IdSMTPServer for Indy 10.
  158. // It lets you control the Received: header that is added to the e-mail.
  159. // If you do not want a Received here to be added, set AReceived := '';
  160. // Formatting 'keys' are available in the received header -- please check
  161. // the IdSMTPServer source for more detail.
  162. end;
  163. end.