Home Download Docs Support About

SMTP Protocol

This article is a quick look at the SMTP protocol using a script to spy on the SMTP conversation. It will give you a good idea about how SMTP works, and it may be useful in troubleshooting any SMTP problems you may have.

 

The SMTP Proxy

Here is some vbscript code that will create a simple socket proxy. Save the code as a .vbs file or you can paste it into VB or VBA.

 

Set sl = CreateObject("Toolsack.SocketListener")
sl.Listen 25 '25 is the port for SMTP
   
Set client = sl.GetNextConnection(60)
   
Set server = CreateObject("Toolsack.Socket")
server.Connect "your.smtphost.com", 25
 
set sleeper = CreateObject("Toolsack.Sleeper")
 
dia = ""
Do
  If client.IsLineAvailable Then
     t = client.ReadLine
     server.Write t & vbCrLf
     dia = dia & "c> " & t & vbCrLf
  elseIf server.IsLineAvailable Then
     t = server.ReadLine
     client.Write t & vbCrLf
     dia = dia & "s< " & t & vbCrLf
  else
     sleeper.sleep 0.1 ' wait .1 seconds
  End If
       
Loop While client.AtEndOfStream = False
 
MsgBox dia
 
Set fi = CreateObject("Toolsack.FileIO")
'fi.SaveFile "C:\temp\smtp.txt", dia, true

 

As you can see the script listens on port 25 (the standard SMTP port). Once it receives a connection, it opens a seconds socket to the real mail server or agent. It then passes lines back and forward, keeping a copy of the line. At the end it displays a dialog with the entire conversation.

The conversation may be too large for a dialog, you may wish to save it to a file instead. In the code above, the file save code is commented out.

 

Sending Mail

Here is a simple script to send email through the proxy. You could use a normal mail client if you set the mail server to the machine that is running the proxy.

 

Set m = CreateObject("Toolsack.SMTPMail")
m.SetFrom "bob@toolsack.com"
m.SetSubject "This is the subject"
m.AddBody "body"
m.AddRecipient "to@toolsack.com"
m.Send "localhost"

 

The result

If everything runs well, you should see something like this:

 
s< 220 mta3-rme.xtra.co.nz ESMTP server ready Mon, 28 May 2001 03:48:20 +1200
c> EHLO mymachine
s< 250-mta3-rme.xtra.co.nz
s< 250-HELP
s< 250-XREMOTEQUEUE
s< 250-ETRN
s< 250-PIPELINING
s< 250-DSN
s< 250-8BITMIME
s< 250 SIZE 104857600
c> MAIL FROM: <bob@toolsack.com> BODY=8BITMIME
s< 250 Sender <bob@toolsack.com> and extensions (BODY=8BITMIME) Ok
c> RCPT TO: <to@toolsack.com>
s< 250 Recipient <to@toolsack.com> Ok
c> DATA
s< 354 Ok Send data ending with <CRLF>.<CRLF>
c> Date: Mon, 28 May 2001 03:48:39 +1200
c> Message-ID: <A3DABCE0A9524DA0AE6E251180B03B22.bob@toolsack.com>
c> From: <bob@toolsack.com>
c> Subject: This is the subject
c> To: <to@toolsack.com>
c> X-Mailer: Toolsack Baseline 1.0 SMTPMail Component
c> MIME-Version: 1.0
c> Content-Transfer-Encoding: 7bit
c> Content-Type: text/plain; charset="us-ascii"
c>
c> body
c> .
s< 250 Message received: 20010527154820.KUUQ304017.mta3-rme.xtra.co.nz@mymachine
c> QUIT
s< 221 mta3-rme.xtra.co.nz ESMTP server closing connection

 

The first thing that happened was a welcome message from the mail server. The server displays its name and the date (yes xtra is our ISP in New Zealand).

The Mail client sends an EHLO message with the clients name. Older clients may send an HELO message instead.

The server then returns the extensions that are available with the server.

The client sends the MAIL FROM command to initiate sending a new message

The server confirms OK.

The client sends the RCPT TO command.

The server confirms OK. If the server doesn't forward mail to that domain you would get an error.

The client sends the DATA command. The Server says OK and then the whole message contents are sent.

Upon receipt of a line with a single dot, the Server confirms the mail has been received OK.

The Client Quits and the Server confirms the closing connection.

 

Conclusion

You now have the code to see what an SMTP conversation looks like. If you do you ever do have SMTP problems you can use the proxy to tap into the conversation.

 

 

 

1999-2002 Toolsack Software Ltd.