Powershell – how to send email

To send email use this cmdlet:

Send-MailMessage [-To] <string[]> [-Subject] <string> [[-Body] <string>] [[-SmtpServer] <string>] -From <string> [-Attachments <string[]>] [-Bcc <string[]>] [-BodyAsHtml] [-Encoding <Encoding>] [-Cc <string[]>] [-DeliveryNotificationOption <DeliveryNotificationOptions>] [-Priority <MailPriority>] [-ReplyTo <string[]>] [-Credential <pscredential>] [-UseSsl] [-Port <int>] [<CommonParameters>]

The Send-MailMessage cmdlet sends an email message from within PowerShell.

You must specify a Simple Mail Transfer Protocol (SMTP) server or the Send-MailMessagecommand fails. Use the SmtpServer parameter or set the $PSEmailServer variable to a valid SMTP server. The value assigned to $PSEmailServer is the default SMTP setting for PowerShell. 

Example 1: Send an email from one person to another person

Send-MailMessage -From 'User01 <user01@fabrikam.com>’ -To 'User02 <user02@fabrikam.com>’ -Subject 'Test mail’

Example 2: Send an attachment

Send-MailMessage -From 'User01 <user01@fabrikam.com>’ -To 'User02 <user02@fabrikam.com>’, 'User03 <user03@fabrikam.com>’ -Subject 'Sending the Attachment’ -Body „Forgot to send the attachment. Sending now.” -Attachments .\data.csv -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer 'smtp.fabrikam.com’

Example 3: Send email to a mailing list

Send-MailMessage -From 'User01 <user01@fabrikam.com>’ -To 'ITGroup <itdept@fabrikam.com>’ -Cc 'User02 <user02@fabrikam.com>’ -Bcc 'ITMgr <itmgr@fabrikam.com>’ -Subject „Don’t forget today’s meeting!” -Credential domain01\admin01 -UseSsl

More info:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-6

1 myśl na “Powershell – how to send email

  1. Wow, wonderful blog layout! How long have you been blogging for?
    you made blogging look easy. The overall look of your site
    is great, as well as the content!

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany.