Database Mail – Validating your SMTP mail server configuration using powershell


Database mail relies on .NET framework’s SMTP client API to send emails. You could write a simple power shell script like this to test out if you are able to send mails using your smtp server

Sample Powershell Code to send email:

 1: $smtpServer = "emailserver"
 2: $smtpPort = 25
 3: $emailFrom = ""
 4: $emailTo = ""
 5: $smtp = new-object Net.Mail.SmtpClient($smtpServer)
 6: $smtp.Port = $smtpPort
 7: $subject = "subject" 
 8: $body = "body " 
 9: $smtp.Send($emailFrom, $emailTo, $subject, $body)

This posting is provided "AS IS" with no warranties, and confers no rights. 
Use of included script samples are subject to the terms specified at
Comments (2)

  1. Marc Mangus says:

    What about if the server requires authentication (like BPOS)? Thanks!

  2. When you configure Database Mail, you will need to provide the credential as the required authentication.  In this Powershell example, it means to set $smtp.Credentials to the authenticated account.