Speak SMTP like a native




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

Comments (9)
  1. Carl says:

    A dinner before you take me home might have been more appropriate. How about a little background such as "Why agents are helpfull" "What they are replacing from the Ex2003" and most importantly of all "Some real world examples that show why they’re so important"

  2. John Hall says:

    It’s worth noting that a lot of other e-mail servers would not be happy if you cut and pasted all those commands in one go – technically you need to wait for responses to each command before you send the next.

  3. Jeff says:

    Many SMTP systems support Pipelining (sending multiple commands without having to wait for a response after each).  Please refer to RFC 2920 for more information.

  4. Daniel Melanchthon says:

    I thin you forgot the brackets:

    ehlo console

    mail from: <sender@contoso.com>

    rcpt to: <recipient@contoso.com>

    Cheers!

    Daniel

  5. ANDREY says:

    Some SMTP servers require the < and > brackets. Exchange is not one of them. At least Exchange 5.5, 2000, and 2003.

  6. Filip says:

    When I try the Mail from command

    I get 530 5.7.0 client was not authenticated

    I’ve turned all authenticated mechanisms for my default receive connector off , but I still get this error…

    any ideas?

  7. juergen says:

    I get an error after specifying the sender:

    550 5.7.1. Client does not have permissions to send as this sender

    any suggestions?

  8. Nate W says:

    Filip, Juergen,

    If you are working on a private network, you will probably find it helpful to disable the client security checks as follows:  Open the Exchange management console, select the Hub or Edge server in question, double-click the default receive connector, select the Authentication tab, and check "externally secured."  If that doesn’t solve the problem, contact me at natew(at)microsoft.com and I’ll see what I can do.

    However if you are working on a server that faces the internet, it might be unwise to disable so much of E12’s security this way.  That’s another subject for another blog.

  9. Anonymous says:

    In the last projects that I have been working I need to rely on an SMTP server with some &quot;security policies&quot;……

Comments are closed.