5,445,109 members and growing! (13,789 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Email     Intermediate

SMTP Login class

By Benjamin L. Miller

Basic SMTP Login class
C#, Windows, .NET 1.0, .NET, Visual Studio, Dev

Posted: 21 Jan 2003
Updated: 21 Jan 2003
Views: 60,208
Bookmarked: 39 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
23 votes for this Article.
Popularity: 5.50 Rating: 4.04 out of 5
2 votes, 8.7%
1
1 vote, 4.3%
2
3 votes, 13.0%
3
6 votes, 26.1%
4
11 votes, 47.8%
5

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 " 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: <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

About the Author

Benjamin L. Miller



Location: United States United States

Other popular Internet / Network articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
Subject  Author Date 
NewsRedesigned code + 3 bug fixes [modified]memberJohnnyUT13:07 30 Jan '08  
GeneralSMTP Login class FIX...membertheBuck10:55 9 Nov '04  
GeneralAuth loginmembertrongnd21:53 30 May '04  
GeneralRe: Auth loginmemberBenjamin L. Miller21:17 10 Jun '05  
GeneralAUTH LOGINmemberAlex Rovner6:36 29 Apr '04  
GeneralRe: AUTH LOGINsussAnonymous16:36 23 Jan '05  
GeneralServer Timeout Notememberyfisaqt20:09 31 Mar '04  
GeneralThank youmembershivpal20:31 14 Oct '03  
GeneralBusy waiting?memberstevex3:04 18 May '03  
GeneralAdding attachmentsmemberrkendall18:19 24 Apr '03  
GeneralCram MD5?memberkalme7:33 29 Jan '03  
GeneralRe: Cram MD5?memberBenjamin L. Miller8:02 30 Jan '03  
GeneralAnother SMTP library with the socketsmemberSebastien Curutchet21:21 22 Jan '03  
GeneralRe: Another SMTP library with the socketsmemberGriffonRL3:40 23 Jan '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Jan 2003
Editor: Nishant Sivakumar
Copyright 2003 by Benjamin L. Miller
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project