SMTP Test from PowerShell

By:   |   Updated: 2020-12-24   |   Comments (3)   |   Related: > Database Mail


Problem

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.

Solution

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 [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
windows powershell

Execute the PowerShell script.  If it is successful, you should get a test email like below.

email

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 
windows powershell

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.

smtp server name

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

telnet client

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
    • Programs
    • 'Turn Windows features on or off'
    • Check 'Telnet Client'
    • OK
  • 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):

telnet

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.

  1. 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
  2. mail from: [email protected]
    • This does not have to be a valid email address, but it does have to be in the [email protected] format
    • It also has to be something your mail filter will not block
    • If successful you will get a '250 Sender [email protected] OK'
  3. 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
  4. subject: email subject (this is optional)
  5. Enter the body of the text then the enter key
  6. . then enter key again and you should see a '250 OK queued as xx' that says the message is queued for delivery
  7. Quit – close connection
command parameters

We should expect to receive the email as seen here.

email

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.
Next Steps

Here are some links to more tips about SMTP and SQL Server:

You can find more information on the SMTP protocol here:



sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Joe Gavin Joe Gavin is from the Greater Boston area. He started working with SQL Server and Sybase in 1998 in the financial services industry and has been a SQL Server Database Administrator for a dairy cooperative since 2011. He graduated from Northeastern University in Boston with a Bachelor of Science in Engineering Technology (BSET) degree in Computer Technology. Joe has spoken at the Boston and Providence SQL Saturday events.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips


Article Last Updated: 2020-12-24

Comments For This Article




Friday, September 24, 2021 - 4:00:26 AM - Niyathi Back To Top (89259)
Hello Joe,

Thank you for your reply. I hope mail filter could be the issue. I am still investigating.

Thursday, September 23, 2021 - 12:12:38 PM - Joe Gavin Back To Top (89257)
Hi Niyathi. If it ran without errors the SMTP server received the request. We'd have to presume it sent it. You may want to try sending it to a different email address or check with whoever administors your mail filter. I've seen mail filters block things you wouldn't expect.

Thursday, September 23, 2021 - 9:42:41 AM - Niyathi Back To Top (89255)
Hello Joe,

Thank you for the tutorial. It was easy to understand. I followed this turorial and was successful with the "Send-MailMessage" command on powershell. I get no errors and it runs successfully without any error message. But I do not get mail in my inbox or Spam or Junk in my Mailbox. I am totally clueless what is happening. Could you please suggest me incase if you know anything?














get free sql tips
agree to terms