banner



How To Test Smtp Services Manually In Windows Server 2012

By:   |   Updated: 2020-12-24   |   Comments (3)   |   Related: More > 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 user@domain.ext 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 user@domain.com -To user@domain.com -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            =            'user@domain.com'            # from address - doesn't have to be valid, just in the format user@domain.ext and something your mail filter will not block            $To            =            'user@domain.ext'            # 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: user@domain.ext
    • This does not have to be a valid email address, but it does have to be in the user@domain.ext format
    • It also has to be something your mail filter will not block
    • If successful you will get a '250 Sender user@domain.ext 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: test@test.com' 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:

  • 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:

  • Simple Mail Transfer Protocol

Related Articles

Popular Articles

About the author

MSSQLTips author Joe Gavin Joe Gavin is from Greater Boston. He has held many roles in IT and is currently a SQL Server Database Administrator.

View all my tips

Article Last Updated: 2020-12-24

How To Test Smtp Services Manually In Windows Server 2012

Source: https://www.mssqltips.com/sqlservertip/6681/smtp-test-from-powershell/

Posted by: moorejusbache.blogspot.com

0 Response to "How To Test Smtp Services Manually In Windows Server 2012"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel