How to Test Email Sending Using PowerShell (Step-by-Step Guide)

Introduction

Testing email functionality from a Windows server or workstation is an essential task for system administrators especially when configuring new SMTP relay servers, alert systems, or application notifications.

PowerShell provides a quick and easy way to send test emails without installing any additional tools. In this guide, you’ll learn how to use the Send-MailMessage cmdlet (and newer secure methods) to test email delivery.

Step 1: Open PowerShell as Administrator

Press Windows + X → Windows PowerShell (Admin) or open PowerShell ISE with admin rights.

Step 2: Basic Email Test Command

If you are using an internal mail relay that doesn’t require authentication (like your IIS SMTP Server), you can use this simple command:

Send-MailMessage `
-From "test@yourdomain.com" `
-To "user@yourdomain.com" `
-Subject "Test Email from PowerShell" `
-Body "This is a test email sent from PowerShell using your internal mail relay." `
-SmtpServer "relay.yourdomain.com"

Explanation:

  • -From: Sender email address
  • -To: Recipient email address
  • -Subject: Email subject line
  • -Body: Message content
  • -SmtpServer: Your relay or mail server hostname/IP

If you’re using port 25 internally, no authentication is required.

Step 3: Send Email with Authentication (e.g., Office 365 or Gmail)

If your mail server requires authentication, use this version:

$SMTPServer = "smtp.office365.com"
$SMTPPort = "587"
$Username = "user@yourdomain.com"
$Password = "YourSecurePassword"
$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ($Username, $SecurePassword)

Send-MailMessage `
-From $Username `
-To "recipient@yourdomain.com" `
-Subject "Test Email via Office 365" `
-Body "This is a test email sent securely from PowerShell." `
-SmtpServer $SMTPServer `
-Port $SMTPPort `
-UseSSL `
-Credential $Cred

Explanation:

  • -UseSSL: Enables TLS/SSL for secure communication.
  • -Port 587: SMTP port for authenticated submission.
  • PSCredential: Stores your username and password securely.

Step 4: Using Gmail SMTP (with App Passwords)

For Gmail, first enable 2-Step Verification and create an App Password for PowerShell. Then use:

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "yourgmail@gmail.com"
$Password = "your_app_password"
$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ($Username, $SecurePassword)

Send-MailMessage `
-From $Username `
-To "recipient@gmail.com" `
-Subject "Gmail SMTP Test" `
-Body "Testing Gmail SMTP using PowerShell." `
-SmtpServer $SMTPServer `
-Port $SMTPPort `
-UseSSL `
-Credential $Cred

Step 5: Attach a File (Optional)

You can include attachments to test file-based email notifications:

Send-MailMessage `
-From "alerts@yourdomain.com" `
-To "admin@yourdomain.com" `
-Subject "Server Log File" `
-Body "Please find the attached log file." `
-Attachments "C:\Logs\serverlog.txt" `
-SmtpServer "relay.yourdomain.com"

Step 6: Verify Delivery

Once sent, check your Inbox and Spam/Junk folders.
You can also monitor your mail relay or mail server logs for delivery confirmation.

Troubleshooting Tips

  • If you get an error like “Send-MailMessage is deprecated”, PowerShell 7 users can install and use the MailKit module as an alternative.
  • Check firewall rules for port 25, 465, or 587.
  • Ensure DNS resolves your SMTP hostname correctly.
  • For Microsoft 365, make sure SMTP AUTH is enabled for your account.