Click here to Skip to main content
15,881,875 members
Articles / Programming Languages / C#
Article

SMTP Login class

Rate me:
Please Sign up or sign in to vote.
4.50/5 (24 votes)
21 Jan 20032 min read 101.8K   722   57   14
Basic SMTP Login class

Introduction

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

  1. Connect to Server
  2. Say Helo
  3. Tell the server you wish to authenticate and how
  4. Tell the server your username (64bit encoded)
  5. Tell the server your password (64bit encoded)
  6. Send the email
  7. 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 <domain>" method.

The only truly important thing to remember here is that the username and password must be 64bit encoded.

C#
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: <youremail@yourdomain.com> <CRLF> 

Next, the server must be told who is to get the message.

RCPT TO: <rcptemail@theirdomain.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

DATA<CRLF> 

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.

.<CRLF> 

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!!!

History

  • This is a first revision

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
NewsRedesigned code + 3 bug fixes [modified] Pin
JohnDoe234530-Jan-08 12:07
JohnDoe234530-Jan-08 12:07 
GeneralSMTP Login class FIX... Pin
ThinkMud9-Nov-04 9:55
ThinkMud9-Nov-04 9:55 
yo, you need to add "\r\n" before the message(at least for my server anyway)
so the send message line should look as follows...

//Send message
SmtpServer.SendData("\r\n" + message + "\r\n");

hope that helps some people!

Thanks to Benjamin L. Miller for the original code!!!

GeneralAuth login Pin
trongnd30-May-04 20:53
trongnd30-May-04 20:53 
GeneralRe: Auth login Pin
Benjamin L. Miller10-Jun-05 20:17
Benjamin L. Miller10-Jun-05 20:17 
GeneralAUTH LOGIN Pin
Alex Rovner29-Apr-04 5:36
professionalAlex Rovner29-Apr-04 5:36 
GeneralRe: AUTH LOGIN Pin
Anonymous23-Jan-05 15:36
Anonymous23-Jan-05 15:36 
GeneralServer Timeout Note Pin
yfisaqt31-Mar-04 19:09
yfisaqt31-Mar-04 19:09 
GeneralThank you Pin
shivpal14-Oct-03 19:31
shivpal14-Oct-03 19:31 
QuestionBusy waiting? Pin
stevex18-May-03 2:04
stevex18-May-03 2:04 
GeneralAdding attachments Pin
rkendall24-Apr-03 17:19
rkendall24-Apr-03 17:19 
QuestionCram MD5? Pin
User 12301629-Jan-03 6:33
User 12301629-Jan-03 6:33 
AnswerRe: Cram MD5? Pin
Benjamin L. Miller30-Jan-03 7:02
Benjamin L. Miller30-Jan-03 7:02 
GeneralAnother SMTP library with the sockets Pin
Sebastien Curutchet22-Jan-03 20:21
Sebastien Curutchet22-Jan-03 20:21 
GeneralRe: Another SMTP library with the sockets Pin
GriffonRL23-Jan-03 2:40
GriffonRL23-Jan-03 2:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.