The previous post talked about how the SmtpSender class will work and showed an example of how you would use this from your code. This post is going to describe the design of the SmtpReceiver class and show you how you can use this in your own code.
The SmtpReceiver class is essentially used to host an SMTP server within your application/service. It will listen for incoming requests on the SMTP port (port 25) and will process those requests appropriatley, sending the relevant responses back to the client. Once a message has been successfully submitted to the SmtpReceiver class (via telnet for example, or from another SMTP server) then an event will be raised that your application can handle. Of course this will be using multi-threading to allow more than one SMTP session to be run at any one time, and there will be properties and other events that your application can use to check the current state of the SmtpReceiver.
Here are the events that I have already begun working on (let me know if there are others that you think would be useful) :
ServerStarted – Raised when the SmtpReceiver successfully starts and is ready to accept incoming connections.
ClientConnected – Raised when an incoming SMTP connection request is accepted
ClientDisconnected – Raised when either the client issues the QUIT command to close the SMTP session or when the connection times out or is otherwise disconnected
CommandReceived – Raised each time an SMTP command (EHLO or MAIL FROM for example) is received from a connected client. Intended to be used if you want your program to do verbose logging of all of the incoming commands.
SmtpEmailReceived – This event is raised when a valid SMTP message is completed and submitted by a connected client. This is the event that you would want to handle as the starting point for processing incoming emails, as the full submitted email will be passed in to this event.
ServerStopped – Raised when the SmtpReceiver stops listening for incoming SMTP requests, usually due to the Stop method being called or the host application closing down.
When would you use this class? Well only when you want to create your own email server really – it could easily be combined with my SmtpSender class to help create your own fully blown SMTP server, as that was (and still is) the primary reason fro me creating these two classes . You could have a server using these two classes that could accept incoming emails from external servers and deliver them to your own custom email software but could also accept requests from internal clients and if the address they are sending to is external to your email system then use the SmtpSender class to fire the email off to the recipient’s email server.