Introduction

Ensuring that your SMTP servers are correctly configured and reachable is critical for reliable email delivery. Whether you’re a network administrator or a system engineer, there may be times when you need to troubleshoot email connectivity issues. In this guide, we will cover several methods to verify if an SMTP server is listening, including checking DNS records, using PowerShell, and using telnet for direct connections.


1. Checking DNS and MX Records with PowerShell

Before checking if an SMTP server is listening, it is crucial to verify that the domain’s DNS and MX records are correctly configured. This ensures that email traffic is properly routed to your mail server.

A. Checking MX Records

Example:


Resolve-DnsName -Name "pingmything.net" -Type MX | Sort-Object -Property Preference

Explanation: This command retrieves the MX records for the specified domain and sorts them by priority. It ensures your mail server is correctly set up to handle incoming emails.

How to Try It: Open a PowerShell window and run the command, replacing pingmything.net with your domain. The output will list the MX records along with their priorities.

B. Resolving IP Addresses of MX Records

To ensure that your MX records point to valid IP addresses, use the following script:


$mxRecords = Resolve-DnsName -Name "pingmything.net" -Type MX
foreach ($mx in $mxRecords) {
    if ($mx.NameExchange) {  # Check if NameExchange is not null or empty
        $ip = Resolve-DnsName -Name $mx.NameExchange -Type A
        Write-Output "$($mx.NameExchange) resolves to IP: $($ip.IPAddress)"
    } else {
        Write-Output "No NameExchange found for one of the MX records"
    }
}

Explanation: This script resolves the IP addresses associated with each MX record. Confirming these IP addresses helps ensure your domain is configured correctly.

How to Try It: Copy the script into a PowerShell window, modify the domain name, and run it. This will return the resolved IP addresses for your MX records.


2. Using PowerShell to Test SMTP Connectivity

After verifying that your DNS and MX records are correctly configured, the next step is to check if the SMTP server is reachable on the necessary ports.

A. Using Test-NetConnection (Basic Connectivity Check)

The Test-NetConnection cmdlet is useful for checking if a specific port is open on a remote server.

Example:


# Check if SMTP is listening on port 25
Test-NetConnection -ComputerName "202.1.168.37" -Port 25

# Check if SMTPS is listening on port 465
Test-NetConnection -ComputerName "202.1.168.37" -Port 465

# Check if SMTP submission is listening on port 587
Test-NetConnection -ComputerName "202.1.168.37" -Port 587

Explanation: If the server is listening on the specified port, the output will include TcpTestSucceeded: True. Otherwise, it will show TcpTestSucceeded: False.

How to Try It: Open a PowerShell window and copy-paste the command, replacing 202.1.168.37 with your server’s IP address.


B. Using TcpClient for a Direct Connection

If you need a more reliable test, especially if Test-NetConnection shows inconsistent results, you can use the .NET TcpClient class in PowerShell.

PowerShell Script Example:


function Test-PortConnection {
    param (
        [string]$IPAddress,
        [int]$Port
    )
    try {
        $tcpClient = New-Object System.Net.Sockets.TcpClient
        $tcpClient.Connect($IPAddress, $Port)
        $tcpClient.Close()
        return $true
    } catch {
        return $false
    }
}

# Test if SMTP is listening on multiple ports
$ip = "202.1.168.37"
$ports = @(25, 465, 587)

foreach ($port in $ports) {
    if (Test-PortConnection -IPAddress $ip -Port $port) {
        Write-Output "Port $port is open on $ip"
    } else {
        Write-Output "Port $port is closed on $ip"
    }
}

How to Try It: Copy the script into a PowerShell window, adjust the $ip variable, and run it. This checks multiple ports for SMTP connectivity.


3. Using telnet to Manually Test SMTP Ports

The telnet command is another tool for manually checking if an SMTP server is listening on a given port. This method is useful if you need a quick and interactive way to test server responses.

Enabling Telnet on Windows

Before using telnet, you may need to enable it on your Windows system:


dism /online /Enable-Feature /FeatureName:TelnetClient

Testing SMTP Ports with Telnet

Command Examples:


telnet 202.1.168.37 25
telnet 202.1.168.37 465
telnet 202.1.168.37 587

How to Try It: Open a command prompt, replace the IP address with your server’s, and run the command. A successful connection will display a response from the server.


Conclusion

Ensuring that your DNS and MX records are correctly configured is the first step in verifying SMTP connectivity. Once confirmed, use PowerShell scripts and telnet to check if your server is reachable on the necessary ports. These methods will help you efficiently diagnose and resolve email-related issues.

References

I hope this guide helps you troubleshoot your SMTP issues effectively!