
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Send Email Using PowerShell
To send email using PowerShell, there are multiple methods but there is a simple command called SendMailMessage. This command is a part of the module called Microsoft.PowerShell.Utility
To send email using the specific SMTP server we need to add the SMTP server parameter.
Send-MailMessage ` -From '[email protected]' ` -To '[email protected]' ` -Subject 'Test Email' ` -SmtpServer 'Smtp.TestDomain.com'
In the above example, an email will be sent from the -From parameter, a user to -To parameter users with the subject name ‘Test Email’ with the specified SMTP server name.
If you have multiple users then you can separate them using a comma and you can also add CC and BCC recipients. For example,
Send-MailMessage ` -From '[email protected]' ` -To '[email protected]','[email protected]' ` -Cc '[email protected]' ` -Bcc '[email protected]' -Subject 'Test Email' ` -Attachments 'C:\Temp\Confidential.pdf' -SmtpServer 'Smtp.TestDomain.com'
In the above example, an attachment will be stored from the location C:\temp.
If your SMTP server requires an SSL connection on the specific port then you can also specify it for example,
Send-MailMessage ` -From '[email protected]' ` -To '[email protected]','[email protected]' ` -Subject 'Test Email' ` -SmtpServer 'Smtp.TestDomain.com' ` -UseSsl -Port 587 -Priority High
We are using here SSL connection on the 587 port with a High Priority email. You can also set Priority to Normal (Default) or Low.
If your server using different SMTP credentials then you can provide credential parameters as well and you can also the Delivery Notification on Delay, Success, Failure, or Never. The default is None.
$creds = Get-Credential Send-MailMessage ` -From '[email protected]' ` -To '[email protected]','[email protected]' ` -Subject 'Test Email' ` -SmtpServer 'Smtp.TestDomain.com' ` -UseSsl -Port 587 -Priority High ` -Credential $creds ` -DeliveryNotificationOption OnSuccess