SMTP Test from PowerShell
Sending emails from a SQL Server Agent Job, Alert or from some other code in SQL Server can be quite handy. The issue is that sometimes the email is not delivered and the question is whether there is a SQL Server issue or an SMTP issue. In this article we look at two ways to test the SMTP server to rule that out as the potential issue.
We will walk through two ways to test SMTP. The first way uses PowerShell and is quick and simple. The second way has a few more steps, but allows you to see and understand a bit more about the process.
Testing SMTP with PowerShell
We will be using the PowerShell Send-MailMessage cmdlet.
Simply edit the line with SMTP server name, port (it will default to 25 if you don't specify it), the from email address (doesn't have to be valid, just in the format us[email protected] and something your mail filter will not block), to email address, subject, and message body as follows.
Send-MailMessage -SmtpServer mysmtp.com -Port 25 -From [email protected] -To [email protected] -Subject test -Body test
Execute the PowerShell script. If it is successful, you should get a test email like below.
Alternatively, you can use this to configure your Send-MailMessage line with variables. This is handy if you want a reusable script for your toolbox.
$SmtpServer = 'mysmtp.com' # SMTP Server name $Port = 25 # SMTP server port number – default is 25 $From = '[email protected]' # from address - doesn't have to be valid, just in the format [email protected] and something your mail filter will not block $To = '[email protected]' # email address to send test to $Subject = 'test' # email subject $Body = 'test' # email body Send-MailMessage -SmtpServer $SmtpServer -Port $Port -From $From -To $To -Subject $Subject -Body $Body
Testing SMTP with Telnet
If the PowerShell test doesn't work for some reason, you can then test using telnet as follows.
Open a Windows command prompt.
In our example, we're trying to connect to an SMTP server at mysmtp.com and want to establish that we at least have some connectivity as a starting point.
Ping SMTP server name as follows.
Here we see mysmtp.com replied successfully. This indicates we were able to resolve the host name of the SMTP server in Domain Name Service (DNS) and have basic connectivity to it.
That's not always the case and the ping may return one of the following:
- Destination Host Unreachable
- There is a problem finding a route to the destination
- This needs to be resolved before continuing
- Ping Request Could Not Find Host...
- Means the IP address cannot be resolved in Domain Name System (DNS)
- Likely culprit could be as simple as the name of the SMTP server being incorrect or mistyped
- Request Timed Out
- May or may not indicate a problem
- It is possible the SMTP server is configured not to respond to a ping
- Worth trying to connect with Telnet before looking at network
The default port for the Telnet client is 23. If we don't specify a port for SMTP, it will fail. In our example the SMTP server is using the default port of 25.
telnet mysmtp.com 25
Note: If you get a "telnet is not recognized as an internal or external command, operable program or batch file" at this step, you probably don't have the Telnet client enabled as it's typically not enabled by default.
There are a few ways to enable telnet:
- Enabling it via the GUI looks a bit different depending on your Windows
version, but the basic steps are the same
- Open Control Panel
- 'Turn Windows features on or off'
- Check 'Telnet Client'
- Use the Windows 'Deployment Image Servicing and Management tool'
from a Windows command prompt
- dism /online /Enable-Feature /FeatureName:TelnetClient
- Use the PowerShell Install-WindowsFeature
cmdlet (Windows Server only) from PowerShell
- Install-WindowsFeature -Name 'Telnet-Client'
If you're successful connecting to the SMTP server your command prompt window will look something like this (this is also a good test to verify connectivity for any port on other devices):
If you get a "Could not open connection to the host, on port 25: Connect failed" you're probably trying to go through a firewall and port 25 is blocked or the SMTP server is using another port. Other ports sometimes used include 465, 587, or 2525.
Note: It's worth mentioning here that the backspace key is not supported in SMTP. I've found it's the easiest to type, but if you do make a typo, just hit return and start the command again. Also, commands are not case sensitive.
- EHLO or HELO to initiate the SMTP session conversation – in this example, I received a '504 Command parameter not implemented' and is not needed on this particular SMTP server but it's always the first thing to type regardless
- mail from: [email protected]
- data – the text that follows is the email itself and you will see the message to start mail input and complete the email with <CR>.<CR>. This tells it to send the email
- subject: email subject (this is optional)
- Enter the body of the text then the enter key
- . then enter key again and you should see a '250 OK queued as xx' that says the message is queued for delivery
- Quit – close connection
We should expect to receive the email as seen here.
If the email is not received, it could be a few things.
- First and easiest thing to check is to see if the email landed in your junk folder. If it did, you've still tested successfully.
- If it's not in your junk folder, it's possible your email filter blocked it. If you don't have access to the filter you may want to try something different after your 'rcpt to:' (as I ran into using a 'rcpt to: [email protected]' writing this tip) before trying to have someone see if the filter blocked it.
- Less likely is the SMTP server itself is not working correctly. Troubleshooting that is outside the scope of this tip and most likely out of your control, but you will be armed with the steps you have taken to work with whoever is responsible for the SMTP server.
Here are some links to more tips about SMTP and SQL Server:
- Setup SQL Server Database Mail to use a Gmail, Hotmail, or Outlook account
- Setting up Database Mail for SQL Server
- Sending email from SQL Server Integration Services (SSIS)
- How to setup SQL Server alerts and email operator notifications
- SQL Server Database Mail configured with the Send Grid Email Service in the Azure Market Place
- Troubleshoot SQL Server Agent Notifications and Database Mail
- Anatomy of a SQL Agent Email Notification
- Enable SQL Server Agent Mail Profile
- SQL Server Reporting Services SSRS 2017 Installation and Configuration Setup
- Configure SQL Server Database Mail on Amazon RDS
You can find more information on the SMTP protocol here:
About the author
View all my tips
Article Last Updated: 2020-12-24