One of the array of diagnostic tests you may find yourself doing when you’re trying to fix a problem sending and receiving messages to a domain is to do a telnet on Port 25 to a mail server you maybe having a problem with and then trying to run though manually issuing the SMTP commands to send a message to this server so you can see what the responses are. A description of this process can be found in this KB there is also a great tool called SMTPDiag which will run this type of test and more. If you want to do the same thing from a Powershell script well your in luck because the .Net framework makes this process pretty easy. Lee Holmes posted this great script for replacing the missing Telnet on Vista. What I’ve done is rework this script so instead of being an interactive telnet script it’s a script that connects to port 25 on a mail server and then runs through issuing Mail FROM and RCPT TO commands and shows the responses back to the command-line. The script itself takes three command line parameters which are the DNS or IP of the server you want to test and a domainname for the domain you want to test to see if the server will receive email for and a domainname to test a send from( The script doesn’t actually send a message as it quits after the RCPT TO command). To run the script you need to use something like
C:\tnetsmtp.ps1 server.com.au recipeintdomain.com sendingdomain.com
(9/10/2006 Updated input parameters thanks to Hector)
I’ve put a downloadable copy of the code here the script itself looks like.
#param([String] $remoteHost =$(throw "Please specify the Target Server"),[String] $domain = $(throw "Please specify the #recipient Domain"),[String] $sendingdomain = $(throw "Please specify the Sending Domain"))
param([String] $remoteHost,[String] $domain, [String] $sendingdomain)
if ($remotehost -eq "" -or $domain -eq "" -or $sendingdomain -eq "") {"Please specify the Target Server, recipient domain and sending domain"
return; }
function readResponse {
while($stream.DataAvailable)
{
$read = $stream.Read($buffer, 0, 1024)
write-host -n -foregroundcolor cyan ($encoding.GetString($buffer, 0, $read))
""
}
}
$port = 25
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
if($socket -eq $null) { return; }
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter($stream)
$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding
readResponse($stream)
$command = "HELO "+ $domain
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$command = "MAIL FROM:"
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$command = "RCPT TO:"
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$command = "QUIT"
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
## Close the streams
$writer.Close()
$stream.Close()
C:\tnetsmtp.ps1 server.com.au recipeintdomain.com sendingdomain.com
(9/10/2006 Updated input parameters thanks to Hector)
I’ve put a downloadable copy of the code here the script itself looks like.
#param([String] $remoteHost =$(throw "Please specify the Target Server"),[String] $domain = $(throw "Please specify the #recipient Domain"),[String] $sendingdomain = $(throw "Please specify the Sending Domain"))
param([String] $remoteHost,[String] $domain, [String] $sendingdomain)
if ($remotehost -eq "" -or $domain -eq "" -or $sendingdomain -eq "") {"Please specify the Target Server, recipient domain and sending domain"
return; }
function readResponse {
while($stream.DataAvailable)
{
$read = $stream.Read($buffer, 0, 1024)
write-host -n -foregroundcolor cyan ($encoding.GetString($buffer, 0, $read))
""
}
}
$port = 25
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
if($socket -eq $null) { return; }
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter($stream)
$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding
readResponse($stream)
$command = "HELO "+ $domain
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$command = "MAIL FROM:
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$command = "RCPT TO:
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
$command = "QUIT"
write-host -foregroundcolor DarkGreen $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)
## Close the streams
$writer.Close()
$stream.Close()