Creating an SMTP Server - Conclusion (Page 5 of 5 )
To test the server in Outlook Express enter "localhost" for both POP3/SMTP servers and use the username and passwords from the database's users table. Note that Outlook Express requires both an SMTP and POP3 server to be running prior to sending messages, so use the code from my previous POP3 Server tutorial to build a POP3 Server and run them both on your machine. Then start sending email messages! You can of course use these programs in a LAN environment or in a company as long as there's a network. I've included an empty MS Access database (available for download at the beginning of this article) for testing purposes.
procedure TForm1.IdSMTPServer1Connect(AContext: TIdContext); begin //idsmtpserver1.Greeting.SetReply(220,'Welcome to Leidago Server'); //logfile.Accept(acontext.Connection); end;
procedure TForm1.IdSMTPServer1RcptTo(ASender: TIdSMTPServerContext; const AAddress: String; var VAction: TIdRCPToReply; var VForward: String); begin
// The following actions can be returned to the server: { rAddressOk, //address is okay rRelayDenied, //we do not relay for third-parties rInvalid, //invalid address rWillForward, //not local - we will forward rNoForward, //not local - will not forward - please use rTooManyAddresses, //too many addresses rDisabledPerm, //disabled permanently - not accepting E-Mail rDisabledTemp //disabled temporarily - not accepting E-Mail } if Pos('@', AAddress) > 0 then begin VAction := rAddressOk; end else begin VAction :=rInvalid; end; end;
procedure TForm1.IdSMTPServer1Received(ASender: TIdSMTPServerContext; var AReceived: String); begin // This is a new event in the rewrite of IdSMTPServer for Indy 10. // It lets you control the Received: header that is added to the e-mail. // If you do not want a Received here to be added, set AReceived := ''; // Formatting 'keys' are available in the received header -- please check // the IdSMTPServer source for more detail. AReceived := ''; end;
procedure TForm1.IdSMTPServer1UserLogin(ASender: TIdSMTPServerContext; const AUsername, APassword: String; var VAuthenticated: Boolean); begin // This event is fired if a user attempts to login to the server // Normally used to grant relay access to specific users etc. //Search for the username and password in "users" table.. q2.SQL.Text := 'SELECT * from users WHERE uname=:user AND upass=:pwd'; q2.Parameters.ParamByName('user').Value :=AUsername; q2.Parameters.ParamByName('pwd').Value := APassword; q2.open; //if the user is not found, set authentication to false if q2.RecordCount = 0 then begin
VAuthenticated := False; end else begin VAuthenticated := True; end;
end;
procedure TForm1.IdSMTPServer1MailFrom(ASender: TIdSMTPServerContext; const AAddress: String; var VAction: TIdMailFromReply); begin // Here we are testing the MAIL FROM line sent to the server. // MAIL FROM address comes in via AAddress. VAction sets the return action to the //server. // The following actions can be returned to the server: { mAccept, mReject }
if Pos('@', AAddress) > 0 then begin VAction:= mAccept;
end
else begin VAction := mReject; end; end;
procedure TForm1.FormCreate(Sender: TObject); begin
idsmtpserver1.Greeting.SetReply(220,'Welcome to SMTP Server'); end;
procedure TForm1.IdSMTPServer1Execute(AContext: TIdContext); begin logfile.DoLogWriteString(acontext.Connection.IOHandler.ReadLn);
end;
procedure TForm1.IdSMTPServer1Exception(AContext: TIdContext; AException: Exception); begin acontext.Connection.IOHandler.Write(aexception.Message); end;
end.
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.