in

ASP.NET Weblogs

Wanta .NET ?

Dave Wanta

System.Web.Mail Explained

System.Web.Mail and SMTP Explained

Building aspNetEmail, aspNetMime, and aspNetMX, has caused me to debug quite a few different email scenarios, for both client and friends.  Over the next few blog entries, I’m going to explore SMTP and System.Web.Mail to explain what is happening.

Lets first start with SMTP. SMTP (Simple Mail Transfer Protocol ) was first introduced around 1982 with RFC 821. Its now been updated to RFC 2821. SMTP is *ONLY* used for sending email. It is not used for retrieving email from the server, or managing email, or parsing it. Its only used for client to server, or server to server communication.  SMTP is totally text based. Which means, if we want to troubleshoot a SMTP session, its as easy as opening a Telnet session to a mail server and issuing commands.

SMTP Commands
Speaking of commands, lets talk about a couple of them. HELO, MAIL FROM, RCPT TO, DATA, RSET, QUIT. These commands are issued during a SMTP Session. The also need to be issued in a certain order.

Each of these commands take a line of text (with the exception of DATA), and are executed with a Carriage Return Line Feed Combination.

DATA is actually executed with a Carriage Return Line Feed . Carriage Return Line Feed combination
[VB]
vbCrLf + "." + vbCrLf
[C#]
"\r\n.\r\n"

After each of these commands are executed, the server will reply with a 3 digit reply, followed by a textual description. From server to server, this description may be different, but the command has to be the same. For a detailed look at the possible replies, check out RFC 2821.

HELO, MAIL FROM, RCPT TO, RSET, QUIT Commands

HELO
HELO is merely used to say "hello" to the server and introduce yourself. And example of the command is
HELO davesLaptop

Here I basically told the mailserver who I was.

MAIL FROM
The next command, in the SMTP session is the MAIL FROM command. This command will tell the server who the mail is from. Its issued like
MAIL FROM: <
dave@aspNetEmail.com>

RCPT TO
The next command, is the RCPT TO command. It tells who the mail will be going too. For example:
RCPT TO: <
you@yourcompany.com>
RCPT TO: <
him@hiscompany.com>
RCPT TO: <
her@hercompany.com>

As you can see, there were multiple RCPT TO commands issued here.  A RCPT TO command must be issued for everyone listed on your ‘TO’, ‘CC’ and ‘BCC’ line of the email.

DATA
The next command issued is DATA, which will actually send the text content of the email. The text content includes all of the email headers and the body.  Because the content contains multiple lines, the DATA command is terminated with a CrLf.CrLf sequence. For example:

DATA
This is my text email. It will
contain
a
few
lines of data


.

(notice the period at the end, on a line by itself? That’s how the DATA command was terminted).

RSET and QUIT
The last two commands that can be issued anytime during the SMTP session are the RSET and QUIT commands. They are executed on a line by themselves. RSET resets the SMTP session back to its original state, and clears any previously issued commands. QUIT gracefully closes the SMTP session.

To see this in action, here is a screen shot of a telnet session between me and my SMTP server.

 

Conclusion
In this blog entry, I’ve explained some of the SMTP commands used during a SMTP session. In the next blog entry, I’ll delve more into the System.Web.Mail namespace.


 

Comments

 

TrackBack said:

ScottW's ASP.NET WebLog
July 9, 2003 2:46 PM
 

TrackBack said:

Wanta .NET ?
July 9, 2003 2:46 PM
 

Roy Osherove said:

Great intro! always wanted to exlpore this area, and now you've sparked my curiosity even more :)
July 9, 2003 4:24 PM
 

Scott Mitchell said:

Dave, let me know if you'd ever like to write an article on 4Guys on stuff like this. I think you could easily tie it into ASP.NET Email. :-) Also, a good thing to do would be an article on POP3. Many people ask, "How can I let my visitors read email through an ASP.NET Web page?"
July 9, 2003 4:49 PM
 

Dave said:

Wow, Mr ISerializeable and Mr. 4GuysFromRolla left me comments. I'm flattered and honored.

Cheers!
Dave
July 9, 2003 5:03 PM
 

Scott Watermasysk said:

Dave,

Speaking of articles, you might want to eventually switch this post over to be an article/story.

You can create a "Mail" or "SMTP" category which will show up on every page of your blog.

Posts are great (since they are syndicated), but the downside is they eventually "fade" away. For items like this, I recommend making a post summarizing the article/story with a link to it.

-Scott
July 9, 2003 8:26 PM
 

Yosi Taguri said:

keep up the great work.
I've used your kbalerts for ages...

July 19, 2003 2:22 PM
 

Brooke said:

Anyone knows how to get the return code(such as 550) if the email is bounced back using System.Web.Mail, or it simply can't be done? Thanks.
August 23, 2003 10:47 PM
 

David Muggleston said:

Dave,

I have used telnet to send test messages for years, and never found a way to specify the subject of the email. Do you know how?

Regards,
David Muggleston
david.muggleston@northdoor.co.uk
September 3, 2003 2:05 PM
 

Marc Kupper said:

> I have ... never found a way to specify the subject of the email.

In the DATA part of the message you have the SMTP header, crlf, crlf, and then the message body. In telnet you would use
DATA
SUBJECT: Testing

Message text
.

Marc
November 4, 2003 11:51 AM
 

manoj said:

How can we authenticate the user in SMTP
March 17, 2004 2:52 AM
 

Dave Wanta said:

March 17, 2004 6:53 AM
 

Greg said:

You have done a great job of explaining the intro parts. Is it possible to add a file attachment to the scenario...???
April 5, 2004 12:28 PM
 

Dave Wanta said:

Can you explain more what you mean by "file attachment" ?

You mean how attachments are handled in email?

Cheers!
Dave
April 5, 2004 12:38 PM
 

vikram said:

can you give commands to attach files to email from telnet. that file is on that server like /etc/passwd. can we do this to email anyfile from that email server.
May 17, 2004 1:28 PM
 

Dave Wanta said:

this can't be done. you have to embed the attachment in the email contents.

remember, we are talking at a network layer, not an application layer.

May 17, 2004 1:34 PM
 

Balaji said:

Can you please tell me like how to read the contents of an incoming email?

August 16, 2007 11:47 PM
 

jignesh said:

How to verify/validate each Recipient before sending mail.

October 5, 2007 3:55 AM
 

sarastro said:

>>As you can see, there were multiple RCPT TO

>>commands issued here.  A RCPT TO command

>>must be issued for everyone listed on

>>your ‘TO’, ‘CC’ and ‘BCC’ line of the email.

So, in order to send the same message to a group of recipients I need to send multiple RCPT TO: commands.

OK, but can you explain, please, how could I distinguish between a 'TO' recipient and a 'BCC' recipient?

November 19, 2007 10:43 AM
 

Mike said:

Hey Hey,

Nice explanation.

As a job interview requirement, I have to make an ASP.Net page that takes an "e-mail address and check it against the mail server for that address using SMTP to determine if it's a valid or invalid mailbox".

Now I have to come up with the code.

February 8, 2008 9:23 PM

Leave a Comment

(required)  
(optional)
(required)  
Add