Monday, March 29, 2010

How to Use Telnet to Send SMTP Email to Exchange 2007 and 2010

Unlike previous versions of Exchange, Exchange Server 2007 and Exchange 2010 use strict RFC compliance to send and receive SMTP email.  This can cause some unexpected behavior if a server or application the sends email to Exchange does not comply with RFC 5322 or RFC 2822.

Many email administrators know how to use the Telnet client to send SMTP email.  In this article I’ll show you how to send RFC compliant email using Telnet.  Doing so also allows you to also add X-Headers to your email, which I’ll explain later.

First, you need to have access to the Telnet client.  This is already installed as part of the operating system on all Windows platforms prior to Windows Vista or Windows 2008.

To install the Telnet Client for Windows Vista and Windows 7:
  1. Open Control Panel > Programs and Features
  2. Select Turn Windows features on or off
  3. In the list that appears, check the box beside Telnet Client
  4. Click OK
For Windows Server 2008 and Windows Server 2008 R2:
  1. Open Server Manager and select Features
  2. Click Add Features
  3. Check Telnet Client, click Next and Install
You may also want to try PuTTY.  PuTTY is a free Telnet/SSH client that has extended features, such as logging and appearance personalization.  More importantly, PuTTY allows you to use the backspace key to correct typing mistakes – something the Telnet client won’t do.

So, on with the demo.  Let’s start first with an example of the way most people use Telnet to send email.  Use your Telnet client to connect to the SMTP server on port 25 and issue the following commands.  I’ve color coded what I type in blue and what the server returns in maroon.
telnet exchange.domain.com 25
220 exchange.domain.com Microsoft ESMTP MAIL Service ready at Sun, 28 Mar 2010 08:49:36 -0700
helo domain.com
250 VLHC.scif.com Hello [10.1.117.29]
mail from:user1@domain.com
250 2.1.0 Sender OK
rcpt to:user2@domain.com
250 2.1.5 Recipient OK
data
354 Start mail input; end with <CRLF>.<CRLF>

Subject:Test email using Telnet
This is a test.
.

250 2.6.0 <9c642f92-e0e3-4b9e-b3d3-21054eed3247@exchange.domain.com> Queued mail for delivery
quit
221 2.0.0 Service closing transmission channel
Connection to host lost.
Assuming that the recipient email address is valid and the configuration on the Exchange server allows the email to be relayed, this results in an email being sent to user2@domain.com.  However, you will notice a few peculiar things about the email that was received.

First, the To: field in the email is blank.  If you look at the SMTP headers, you will see that the To: header shows “Undisclosed recipients:;”, as shown below.


The second issue with the email is that the body of the message is blank.  It should show “This is a test.

Here’s how to send that same email using RFC 2822 compliance to have it displayed correctly in Outlook.  Use your Telnet client to connect to the SMTP server on port 25 and issue the following commands.  This time I've used the color green to indicate the changes.
telnet exchange.domain.com 25
220 exchange.domain.com Microsoft ESMTP MAIL Service ready at Sun, 28 Mar 2010 08:52:17 -0700
helo domain.com
250 VLHC.scif.com Hello [10.1.117.29]
mail from:user1@domain.com
250 2.1.0 Sender OK
rcpt to:user2@domain.com
250 2.1.5 Recipient OK
data
354 Start mail input; end with <CRLF>.<CRLF>

to:user2@domain.com
Subject:Test email #2 using Telnet
          <
---This is a CRLF
This is a test.
.

250 2.6.0 <10c637ec-f8fb-4753-85c4-76d5c4f76108@exchange.domain.com> Queued mail for delivery
quit
221 2.0.0 Service closing transmission channel
Connection to host lost.
This results in an email where the To: field is displayed correctly in both the SMTP headers and the email itself, and the body of the message is displayed, as shown below.


The difference between the two emails are the “to:user2@domain.com” immediately after the “data” command and before the “Subject:…” command.  There must also be a blank line after the
Subject and before the body of message is entered.

Technically, the to: line can be entered as either to:user2@domain.com or to:John Smith <user2@domain.com>.  Outlook will attempt to auto-resolve the To: line to the display name stored in Active Directory and will overwrite the display name entered.  Keep in mind that the
To: command does not have to match the address used in the RCPT command. If the address entered cannot be resolved, Outlook will display the display name listed on the To: line, or just the email address if none was entered.

If you want to add a custom X-Header, you add it anytime after the DATA command and before the Subject:... command.  For example:
data
354 Start mail input; end with <CRLF>.<CRLF>
to:user2@domain.com
X-My-Test-Header:My Data

Subject:Test email with custom X-Header
This results in the X-My-Test-Header: My Data being added to the SMTP header, as shown below:


X-Headers are often used to add metadata to an email message.  Antivirus and anti-spam solutions usually add some form of X-Header to emails to show what type of processing has occurred and possibly what score has been applied to the message.
You can use the X-Headers to create Hub Transport rules that act upon messages in a particular way. For example, you can set the Spam Confidence Level (SCL) on a message based on the anti-spam solution's score, reported in an X-Header.

You can also use this method to test that the X-Headers are promoted properly in Exchange. You can read more about named property promotion here.

12:46 PM