Use Powershell to send test emails (o365 example)

Created by Jeremy Burgess, Modified on Wed, 15 Feb, 2023 at 10:06 AM by Jeremy Burgess

Symptoms

Situation - an application is failing to send emails from a given computer. You need to test whether emailing from that computer works at all. You may be creating a script to send emails from the command line.

Cause

  • Possible firewall restrictions
  • Incorrect server details
  • Incorrect credentials

Resolution

Use a Powershell script to send emails directly from the command line. This will ensure that the correct server/credentials are being used and will help to isolate firewall issues as a cause.

$accountEmail = 'sendersemail@server.com'
$accountPassword = ConvertTo-SecureString 'thisisyourpassword' -AsPlainText -Force

$cred = New-Object System.Management.Automation.PSCredential ($accountEmail, $accountPassword)

Send-MailMessage -From $accountEmail -Subject "Test email" -To notifications@drflow.co.uk -Body "Testing email functionality" -Credential $cred -Port 587 -SmtpServer smtp.office365.com -UseSsl

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article