Power shell send mailmessage
PowerShell provides a built-in cmdlet called Send-MailMessage
that allows you to send an email message. Here is an example of how to use it:
Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Test Email" -Body "This is a test email"
This will send an email message to the recipient at [email protected]
from the sender at [email protected]
with the subject "Test Email" and a body that says "This is a test email".
You can also specify additional parameters to customize the email message, such as:
-SmtpServer
: specifies the SMTP server to use for sending the email-Attachments
: specifies one or more files to attach to the email-Priority
: specifies the priority of the email (High, Normal, Low)-BodyAsHtml
: specifies whether the email body should be sent as HTML or plain text-Encoding
: specifies the encoding of the email body
Here is an example that includes some of these additional parameters:
Send-MailMessage -To "[email protected]" -From "[email protected]" -Subject "Test Email" -Body "This is a test email" -SmtpServer "smtp.example.com" -Attachments "C:\Path\To\File1.txt", "C:\Path\To\File2.txt" -Priority High -BodyAsHtml -Encoding UTF8
This will send an email message to the recipient at [email protected]
from the sender at [email protected]
with the subject "Test Email" and a body that says "This is a test email". The email will be sent using the SMTP server at smtp.example.com
, and will include two attachments: File1.txt
and File2.txt
. The email will have a high priority, and the body will be sent as HTML with UTF-8 encoding.
Note that you need to have the Send-MailMessage
cmdlet installed and configured on your system in order to use it. If you are using PowerShell 3 or later, you can install the Send-MailMessage
cmdlet by running the following command:
Install-Module -Name SendMail
If you are using PowerShell 2, you can use the Send-MailMessage
cmdlet that is included with the.NET Framework.