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:
- -To: The email address or addresses of the recipients.
- -Subject: The subject of the email message.
- -Body: The body of the email message.
- -From: The email address of the sender.
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