Send mail in powershell
In PowerShell, you can send an email using the Send-MailMessage
cmdlet. Here's a basic example:
Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Test Email" -Body "This is a test email sent from PowerShell"
You can also specify additional parameters, such as:
-SmtpServer
: The SMTP server to use for sending the email.-Attachments
: One or more files to attach to the email.-Priority
: The priority of the email (High, Normal, Low).-BodyAsHtml
: Whether to send the email body as HTML or plain text.
Here's an example with some additional parameters:
Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Test Email" -Body "This is a test email sent from PowerShell" -SmtpServer "smtp.example.com" -Attachments "C:\Path\To\File1.txt", "C:\Path\To\File2.pdf" -Priority High -BodyAsHtml $true
Make sure to replace the placeholders with your actual email addresses, SMTP server, and file paths.
If you're using Exchange Server, you can use the Send-MailMessage
cmdlet with the -UseExchange
parameter to send the email through Exchange:
Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Test Email" -Body "This is a test email sent from PowerShell" -UseExchange
Note that the Send-MailMessage
cmdlet is only available in PowerShell 3 and later versions. In PowerShell 2, you can use the Send-MailMessage
cmdlet from the Microsoft.PowerShell.Mail
module, which is included with PowerShell 2.