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.