Skip to content

Test SMTP connection with telnet PowerShell script

How to test SMTP communication with PowerShell? After you enable the SMTP service on the firewall for a specific system or configure an anonymous SMTP relay, you like to check that SMTP works. Telnet is your choice. But, what if you can use telnet wrapped in PowerShell? In this article, you will learn how to test SMTP communication with telnet PowerShell script.

Before you start

It’s good to verify that:

  • No antivirus or security products block SMTP connections
  • No firewall blocks SMTP connections

Download SMTP Telnet PowerShell script

Download the Test-SMTP-Telnet.ps1 PowerShell script or copy and paste the below code in Notepad. Give it the name Test-SMTP-Telnet.ps1 and place it in the C:\scripts folder. Create a script folder if you don’t have one.

param (
    [Parameter(Mandatory = $true)] 
    [string]$RemoteHost, # Exchange Server host name or IP address

    [Parameter(Mandatory = $true)] 
    [string]$Port, # Choose port number 25 or any other

    [Parameter(Mandatory = $true)] 
    [string]$From, # Sender email address

    [Parameter(Mandatory = $true)] 
    [string]$To, # Recipient email address

    [Parameter(Mandatory = $true)] 
    [string]$Greet, # Choose between HELO or EHLO

    [Parameter(Mandatory = $false)]
    [string]$Subject  # Subject
)

function readResponse {
    while ($stream.DataAvailable) {
        $read = $stream.Read($buffer, 0, 1024)
        write-host -n -foregroundcolor cyan ($encoding.GetString($buffer, 0, $read))
        ""
    }
}

$socket = new-object System.Net.Sockets.TcpClient($RemoteHost, $Port)

if ($null -eq $socket) { 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)

write-host -foregroundcolor yellow $Greet
""
$writer.WriteLine($Greet)
$writer.Flush()
start-sleep -m 500
readResponse($stream)

$command = "MAIL FROM: $from"
write-host -foregroundcolor yellow "MAIL FROM: $from"
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)

$command = "RCPT TO: $To"
write-host -foregroundcolor yellow $command
""
$writer.WriteLine($command)
$writer.Flush()
start-sleep -m 500
readResponse($stream)

if ($Subject -eq "") {

    $command = "QUIT"
    write-host -foregroundcolor yellow $command
    ""
    $writer.WriteLine($command)
    $writer.Flush()
    start-sleep -m 500
    readResponse($stream)
    $writer.Close()
    $stream.Close()

}
else {

    $command = "DATA"
    write-host -foregroundcolor yellow $command
    ""
    $writer.WriteLine($command)
    $writer.Flush()
    start-sleep -m 500
    readResponse($stream)

    write-host -foregroundcolor yellow "Subject : $Subject"
    ""
    $writer.WriteLine("subject:$Subject `r")
    $writer.Flush()
    start-sleep -m 500
    readResponse($stream)

    write-host -foregroundcolor yellow "."
    ""
    $writer.WriteLine(".")
    $writer.Flush()
    start-sleep -m 500
    readResponse($stream)

    $command = "QUIT"
    write-host -foregroundcolor yellow $command
    ""
    $writer.WriteLine($command)
    $writer.Flush()
    start-sleep -m 500
    readResponse($stream)
    $writer.Close()
    $stream.Close()
}

After preparing the SMTP PowerShell script, you will test the SMTP connection with EHLO, and HELO commands.

Test SMTP connection

You like to know that SMTP works, and you need to test the SMTP connection. In this example, we will test the anonymous SMTP relay with port 25. You can enter a different port than port 25 and test the port that you like.

It’s important to run the SMTP Telnet PowerShell script on the client itself and not on the Exchange Servers.

For example, you add the Windows Server AP01-2016 IP address 192.168.1.60 to the Exchange Server’s SMTP relay. The AP01-2016 server is an application server that sends emails to internal and external recipients. Sign in on this server and run the Telnet PowerShell script.

Test SMTP connection with telnet PowerShell script remote IP address

In the internal DNS, an A-record is set up for relay.exoip.com that will translate to the load balancer. If you only have one Exchange Server or DNS round-robin, you most likely have the A-record pointed to your Exchange Server.

Test SMTP connection with telnet PowerShell script internal DNS A-record

Use the Exchange Server hostname, IP address, or the DNS record. In this example, relay.exoip.com will resolve to one of the Exchange Servers IP addresses.

HELO SMTP command

Start PowerShell on the system that is allowed to send an email. Start with the HELO command. It looks great because the sender and recipient response codes are both OK.

PS C:\> PowerShell.exe -ExecutionPolicy Bypass C:\scripts\Test-SMTP-Telnet.ps1 -RemoteHost "relay.exoip.com" -Port "25" -From "noreply@exoip.com" -To "john.doe@gmail.com" -Greet "HELO"
220 EX01-2016.exoip.local Microsoft ESMTP MAIL Service ready at Sat, 19 Dec 2020 18:52:54 +0100

HELO

250 EX01-2016.exoip.local Hello [192.168.1.54]

MAIL FROM: noreply@exoip.com

250 2.1.0 Sender OK

RCPT TO: john.doe@gmail.com

250 2.1.5 Recipient OK

QUIT

221 2.0.0 Service closing transmission channel

The below output is what you get when it’s not working. As you can see, there will be no response code 250 2.1.5 Recipient OK. Check your relay receive connector.

PS C:\> PowerShell.exe -ExecutionPolicy Bypass C:\scripts\Test-SMTP-Telnet.ps1 -RemoteHost "relay.exoip.com" -Port "25" -From "noreply@exoip.com" -To "john.doe@gmail.com" -Greet "HELO"
220 EX01-2016.exoip.local Microsoft ESMTP MAIL Service ready at Sat, 19 Dec 2020 18:55:32 +0100

HELO

250 EX01-2016.exoip.local Hello [192.168.1.54]

MAIL FROM: noreply@exoip.com

250 2.1.0 Sender OK

RCPT TO: john.doe@gmail.com

QUIT

EHLO SMTP command

Do it again, but this time with EHLO. The response code 250 2.1.0 and 250 2.1.5 shows that the SMTP connection is good. If you don’t see those response codes, check your SMTP configuration.

PS C:\> PowerShell.exe -ExecutionPolicy Bypass C:\scripts\Test-SMTP-Telnet.ps1 -RemoteHost "relay.exoip.com" -Port "25" -From "noreply@exoip.com" -To "john.doe@gmail.com" -Greet "EHLO"
220 EX01-2016.exoip.local Microsoft ESMTP MAIL Service ready at Sat, 19 Dec 2020 18:56:22 +0100

EHLO

250-EX01-2016.exoip.local Hello [192.168.1.54]
250-SIZE 37748736
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250 CHUNKING

MAIL FROM: noreply@exoip.com

250 2.1.0 Sender OK

RCPT TO: john.doe@gmail.com

250 2.1.5 Recipient OK

QUIT

221 2.0.0 Service closing transmission channel

SMTP port 25 not reachable

The following error shows up if port 25 is not reachable.

PS C:\> PowerShell.exe -ExecutionPolicy Bypass C:\scripts\Test-SMTP-Telnet.ps1 -RemoteHost "relay.exoip.com" -Port "25" -From "noreply@exoip.com" -To "john.doe@gmail.com" -Greet "HELO"
new-object : Exception calling ".ctor" with "2" argument(s): "A connection attempt failed because the connected party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond 192.168.1.54:25"
At C:\scripts\Test-SMTP-Telnet.ps1:29 char:11
+ $socket = new-object System.Net.Sockets.TcpClient($RemoteHost, $Port)
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

HELO SMTP command and sent an email to recipient

Send an email and verify that the message shows up in the recipient’s inbox. Make use of the parameter -Subject.

PS C:\> PowerShell.exe -ExecutionPolicy Bypass C:\scripts\Test-SMTP-Telnet.ps1 -RemoteHost "relay.exoip.com" -Port "25" -From "noreply@exoip.com" -To "john.doe@gmail.com" -Greet "HELO" -Subject "Testing"
220 EX01-2016.exoip.local Microsoft ESMTP MAIL Service ready at Sat, 19 Dec 2020 19:00:19 +0100

HELO

250 EX01-2016.exoip.local Hello [192.168.1.54]

MAIL FROM: noreply@exoip.com

250 2.1.0 Sender OK

RCPT TO: john.doe@gmail.com

250 2.1.5 Recipient OK

DATA

354 Start mail input; end with <CRLF>.<CRLF>

Subject : Testing

.

250 2.6.0 <2af0b0c4-88c0-48a3-992e-13bf89359295@EX01-2016.exoip.local> [InternalId=3496103378945, Hostname=EX01-2016.exoip.local] 1519 bytes in 0.131, 11,274 KB/sec Queued mail for delivery

QUIT

221 2.0.0 Service closing transmission channel

If you don’t get an email, check your SPAM folder or spam filter.

Read more: Exchange SMTP high availability with Kemp load balancer »

Conclusion

In this article, you learned how to test SMTP connection with telnet PowerShell script. It’s essential to save the STMP Telnet PowerShell script on the client/server you want to test on. Fill in the parameters and run the PowerShell script.

Did you enjoy this article? You may also like Exchange Server OWA your connection is not secure. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 2 Comments

  1. Hi Ali

    i have configured this topic . All is well but i got a answer from Load Balancer IP, not from my Virtual IP address. What i did incorrect and we can i change in LB.
    For Example my Load Balancer Ip is : 10.2.1.1
    my Virtual Ip is : 10.2.1.2
    and i got answer from 10.2.1.1… i checked my dns it s correct mail.domain.com points my virtual Ip address .
    Thank you advance for your antwort

Leave a Reply

Your email address will not be published. Required fields are marked *