If you are like me, you have found many SMTP classes and none have dealt with Auth Login. Those that do deal with Auth Login usually come with a fee. Using some java code that I found and the SMTP RFC , I have hashed together a very basic SMTP class that handles Auth Login.
Using the code
First lets look at how SMTP works. The RFC can be found here: RFC
- Connect to Server
- Say Helo
- Tell the server you wish to authenticate and how
- Tell the server your username (64bit encoded)
- Tell the server your password (64bit encoded)
- Send the email
- Disconnect from the server.
Connecting to the server
There are several different methods of authentication available. And the method your server uses may differ from the one I show here. Telneting to the server on port 25, you can type "ehlo" as your greeting and you will receive a list of the valid authentication types back. I do this as my greeting watching for "250 OK" before I continue. On some servers you may not be able to use the "ehlo" command as your primary greeting and should modify the class to use " HELO " method.
The only truly important thing to remember here is that the username and password must be 64bit encoded.
string EncodedUserName = Convert.ToBase64String(Encoder.GetBytes(UserName));
string EncodedPassword = Convert.ToBase64String(Encoder.GetBytes(Password));
Once authenticated to the server you can now begin sending the message. There are several steps in sending a message. First you have to tell the server who is sending the message. On most servers this must be the same person that logged in.
MAIL FROM: <firstname.lastname@example.org> <CRLF>
Next, the server must be told who is to get the message.
RCPT TO: <email@example.com> <CRLF>
Sending the Message
The server now knows who it is receiving mail from and who it should send the mail to. However it does not know what to send. To tell the server that it is about to receive the message we must send the "DATA" tag
The server now knows that it is receiving message data. There are several commands that are valid here. I am only going to cover sending a subject and the actual message.
To send the subject, simply send the "Subject: " command followed by a string.
Subject: <Message Subject><CRLF>
At this point you are ready to send the message. Simply send it as single string to the server. Not much else to it. Remember to follow the message with a <CRLF> to tell the server your done.
Closing the Message
Now that the server has the message, we have send it on it's merry way. This is done by sending a single period to the server.
Points of Interest
The source code that I have for download also includes my basic server class. I know it's not the best so please don't flame for it :-) Enjoy!!!