Search Tools Links Login

Sending Mail from PowerShell

Posted: 2022-12-22
By: dwirch
Viewed: 80

Filed Under:

Powershell, PowerShell Code Cache, Scripting, Tip, Tutorial

No attachments for this post


With Send-MailMessage, you can send email from your scripts. This is especially handy when you have some automated processes, say for monitoring. You could have a script that runs via Task Scheduler that checks for disk space utilization, and sends an email if the disk space is below a threshold.

To use the Send-MailMessage cmdlet in PowerShell, you need to specify the following parameters:

You also need to specify the SMTP server that you want to use to send the email. You can do this using the -SmtpServer parameter.

Here is an example of how you can use the Send-MailMessage cmdlet to send an email:

$To = "recipient@example.com"
$Subject = "Test Email"
$Body = "This is a test email sent from PowerShell."
$From = "sender@example.com"
$SMTPServer = "smtp.example.com"

Send-MailMessage -To $To -Subject $Subject -Body $Body -From $From -SmtpServer $SMTPServer

Note that you may need to configure your SMTP server settings, such as the port number and whether or not to use SSL/TLS, depending on your server's requirements. You can do this using the -Port and -UseSsl parameters.

For example:

Send-MailMessage -To $To -Subject $Subject -Body $Body -From $From -SmtpServer $SMTPServer -Port 587 -UseSsl $true

For more information about the Send-MailMessage cmdlet and its parameters, you can use the Get-Help cmdlet to access the cmdlet's documentation:

Get-Help Send-MailMessage


Comments on this post

No comments have been added for this post.

You must be logged in to make a comment.