If you are writing an SMTP agent, you might find it instructive to learn to speak SMTP yourself, both to aid debugging and to get a feel for what is happening "under the hood" when Exchange receives a message from the internet.  This is a lot easier than it might sound.  Open the Run dialog box (press Window + R) and enter "telnet localhost smtp" (or substitute the name of your Gateway server in place of localhost).  This will open a window where you can talk with the server.  The server will begin the conversation by sending a banner, such as:

 

220 mail.contoso.com ready at Thu, 23 Feb 2006 17:46:23 -0800

 

You may continue the conversation by entering the following commands, one line at a time.  Note that there is a blank line in the middle - it's important.  Just press enter to send the blank line:

 

ehlo console

mail from: sender@contoso.com

rcpt to: recipient@contoso.com

data

To: recipient@contoso.com

From: sender@contoso.com

Subject: Test Message

 

This a test message.

.

quit

 

The server will respond with a simple text message after the first three commands.  After the "data" command, the server will just listen patiently until you enter the "." on a line by itself.  When you initially connect to the server, your agent will be created and the OnConnect event will be fired.  Additional events are fired are follows:

 

ehlo console

This fires OnEhloCommand

 

mail from: sender@contoso.com

This fires OnMailCommand

 

rcpt to: recipient@contoso.com

This fires an OnRcptCommand.  You can repeat this command as many times as you want, specifying a different email address each time.

 

data

This fires OnDataCommand.  The next three lines are not SMTP commands, they are the headers of the message that you are sending.

 

To: recipient@contoso.com

From: sender@contoso.com

Subject: Test Message

(blank line)

The blank line is important, it separates the headers from the content of the message.  When the blank line is received, OnEndOfHeaders fires.

 

This a test message.

.

The "." on a line by itself indicates the end of the message, and this is when OnEndOfData fires.

 

quit

This is the polite way to end the conversation. OnDisconnect will fire at this point, but it will also fire if you end the connection impolitely (for example by closing the window).

 

You can also copy/paste the whole batch of commands - I put them in a text file so I can replay an entire SMTP conversation quickly using only notepad and telnet.   This can be really handy for testing.

 

- Nate Waddoups