How to send email using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

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 'User1@TestDomain.com' `
   -To 'User2@TestDomain.com' `
   -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 'User1@TestDomain.com' `
   -To 'User2@TestDomain.com','User3@TestDomain.com' `
   -Cc 'Manager1@Testdomain.com' `
   -Bcc 'Manager2@testdomain.com'
   -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 'User1@TestDomain.com' `
   -To 'User2@TestDomain.com','User3@TestDomain.com' `
   -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 'User1@TestDomain.com' `
   -To 'User2@TestDomain.com','User3@TestDomain.com' `
   -Subject 'Test Email' `
   -SmtpServer 'Smtp.TestDomain.com' `
   -UseSsl -Port 587 -Priority High `
   -Credential $creds `
   -DeliveryNotificationOption OnSuccess
raja
Published on 04-Jan-2021 09:51:09
Advertisements