Skip to main content

Use Powershell to send test emails (o365 example)

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