PowerShell: Sending Email from a Script
The One Thing To Know First
Send-MailMessage is obsolete. Microsoft has formally marked it obsolete and recommends against using it in new work, primarily because it cannot do modern authentication — no OAuth2, so it will not work with Microsoft 365 or Gmail without an app password, and app passwords are being retired.
It still functions for a plain SMTP relay with a username and password, which is the case in a home lab or a small self-hosted setup. That is the use case this article covers, with the caveat stated rather than buried.

A Working Send
$UserName = '[email protected]'
$Password = Get-Content .\SecurePassword.txt | ConvertTo-SecureString
$Credential = New-Object System.Management.Automation.PSCredential($UserName, $Password)
Send-MailMessage `
-To '[email protected]' `
-From '[email protected]' `
-Credential $Credential `
-SmtpServer 'smtp.techgeek.biz' `
-Port 587 `
-UseSsl `
-Subject 'Backup completed' `
-Body "Backup script completed at $(Get-Date -Format 'yyyy-MM-dd HH:mm')"
The version of this snippet that has been floating around for years is missing a quote (-Subject "Backup -Body "Backup script completed"), which fails with a confusing parse error. Check your quoting if you copied it from somewhere.
Port And Encryption, Which Trips People Up
| Port | What it is | Parameters |
|---|---|---|
| 587 | submission with STARTTLS | -Port 587 -UseSsl |
| 465 | implicit TLS | -Port 465 -UseSsl |
| 25 | legacy relay, often blocked | usually no -UseSsl |
-UseSsl in Send-MailMessage means "use TLS", and on 587 the connection starts plaintext and upgrades. If your relay requires STARTTLS on 587 and you omit -UseSsl, the authentication attempt is rejected — usually with a 5.7.0 or "authentication required" error that suggests your password is wrong.
Attachments And HTML
Send-MailMessage -To '[email protected]' -From '[email protected]' `
-Subject 'Nightly report' `
-Body (Get-Content C:\Logs\report.html -Raw) `
-BodyAsHtml `
-Attachments 'C:\Logs\report.html' `
-SmtpServer 'smtp.techgeek.biz' -Port 587 -UseSsl -Credential $Credential
-BodyAsHtml with Get-Content -Raw is how you send a formatted report. Without -Raw, Get-Content returns an array of lines and the HTML arrives with the whitespace mangled.
Attachments over a few megabytes often fail on a relay's size limit, and the error is a generic send failure. If a script that sends small emails suddenly stops working when the log grows, that is the cause.

Gmail And Microsoft 365
Neither will accept a plain password any more in most configurations.
Microsoft 365 requires modern auth. Basic SMTP AUTH can be enabled per-mailbox by an administrator, but Microsoft has been steadily disabling it and you should not build new automation on it.
Gmail requires an app password, and only if 2-Step Verification is enabled on the account. It works, it is fragile, and Google has been narrowing it.
If your script must send through either, use an SMTP relay service with an API key, or call a mail API directly. In PowerShell that is a Invoke-RestMethod call rather than a cmdlet, and it needs no dependency.
What To Use Instead
For a home lab: Send-MailMessage against your own relay is fine. It is deprecated, not broken, and it does the job for a nightly notification.
For anything that matters: use a mail API. One Invoke-RestMethod to a transactional provider gives you retries, delivery status and no credential gymnastics:
$body = @{
from = '[email protected]'
to = '[email protected]'
subject = 'Backup completed'
text = 'Backup finished at ' + (Get-Date)
} | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri 'https://api.yourprovider.com/v3/messages' `
-Headers @{ Authorization = "Bearer $env:MAIL_API_KEY" } `
-ContentType 'application/json' -Body $body
For a .NET project, use System.Net.Mail.SmtpClient (also legacy) or MailKit, which is the maintained option.

Practical Rules
Never store the password in the script — use PSCredential with an encrypted file, or better, an API key in an environment variable scoped to the service account.
Set a timeout. A dead relay makes a script hang for minutes, and a scheduled task that hangs eventually gets killed with no notification — which is the failure you most want to know about.
Send the failure, not just the success. A notification script that only emails on success tells you nothing when it stops running. Wrap the whole job and email on the exception too, or you will discover a silent failure weeks later.
Don't trust the download button. Trust the hash.
If this was useful, More hands-on tech guides at TechGeek. No pressure — read it whenever it's useful.