Send-MailMessage

Send an email message.

Syntax
      Send-MailMessage [-To] string[] [-Subject] string -From string
         [[-Body] string] [[-SmtpServer] string] [-Attachments string[]]
            [-Bcc string[]]  [-Cc string[]] [-BodyAsHtml]
               [-Credential PSCredential] [-DeliveryNotificationOption NotifyOptions]
                  [-Encoding Encoding] [-Priority {Normal | Low | High}]
                     [-UseSSL] [-Port Int32] [CommonParameters]

Key
   -Attachments string[]
       The path and file names of files to be attached to the email message.
       Use this parameter or pipe the path/file names.

   -Bcc string[]
       Email addresses that receive a copy of the mail but are not listed as recipients of the message.
       Enter names (optional) and the email address, such as "Name <someone@example.com>"

   -Body string
       The body (content) of the email message.

   -BodyAsHtml
       Indicates that the value of the Body parameter contains HTML.

   -Cc string[]
       Email addresses to which a carbon copy (CC) of the email message is sent.
       Enter names (optional) and the email address, such as "Name <someone@example.com>".

   -Credential PSCredential
       A user account that has permission to perform this action. The default is the current user.

       Type a user name, such as "User64" or "Domain64\User64". Or, enter a PSCredential object,
       such as one from the Get-Credential cmdlet.

   -DeliveryNotificationOption
       Delivery notifications (if accepted by the recipient) will be passed back to the email address
       specified in the -From parameter. The alias for this parameter is "-dno".

       NotifyOptions:

          None        No notification.
          OnSuccess   Notify if the delivery is successful.
          OnFailure   Notify if the delivery is unsuccessful.
          Delay       Notify if the delivery is delayed.
          Never       Never notify.

   -Encoding Encoding
       The encoding used for the body and subject. 'unicode' will send UTF16.
       Valid values are ASCII, UTF8, UTF7, UTF32, Unicode, BigEndianUnicode, Default, and OEM.

   -From string
       The address from which the mail is sent.
       Enter a name (optional) and email address, such as "Name <someone@example.com>". This parameter is required.

   -Port
       An alternate port on the SMTP server.
       The default value is 25, which is the default SMTP port.
       The standard secure SMTP port is 587.
       This parameter is available in Windows PowerShell 3.0 and newer releases.

   -Priority MailPriority
       The priority of the email message.
       The valid values for this are Normal, High, and Low.

   -SmtpServer string
       The name of the SMTP server that sends the email message.

       The default value is the value of the $PSEmailServer preference variable.
       If the preference variable is not set and this parameter is omitted, the command fails.

   -Subject string
       The subject of the email message. This parameter is required.

   -To string[]
       The addresses to which the mail is sent.
       Enter names (optional) and the email address, such as "Name <someone@example.com>". Required.

   -UseSSL
       Use the Secure Sockets Layer (SSL) protocol to establish a connection to the remote computer 
       to send mail. By default, SSL is not used, though most mail servers now require it.

Under Windows, send-mailmessage will create ASCII output by default.
This can be configured via the -Encoding option or by setting the $PSDefaultParameterValues preference variable.
Under PowerShell Core edition, the encoding defaults to BOM-less UTF-8

Examples

Send an email (with no body) from User01 to User02:

PS C:\> send-mailmessage -to "User01 <user01@example.com>" -from "User02 <user02@example.com>" -subject "Test mail" -smtpServer smtp.example.com -UseSsl -Port 587

Send an email with an attachment to two users and BCC another:

PS C:\> send-mailmessage -from "me@example.com" -to "user01@example.com", "user02@example.com" -bcc helpdesk@example.com -subject "Hello World" -body "See attachment below." -Encoding UTF8 -Attachment "data.csv" -smtpServer smtp.example.com -UseSsl -Port 587

Send an email message from User01 to the ITGroup mailing list with a copy (CC) to User02 and a blind carbon copy (BCC) to the IT manager (ITMgr):

PS C:\> send-mailmessage -to "ITGroup <itdept@example.com>" -from "User01 <user01@example.com>" -cc "User02 <user02@example.com>" -bcc ITMgr <itmgr@example.com> -subject "Don’t forget today's meeting!" -credential domain64\admin01 -Encoding UTF8 -smtpServer smtp.example.com -UseSsl -Port 587

Send an email message from User01 to the ITGroup mailing list using a job, this ensures the connection will be disposed once it finishes:

PS C:\> $mailBody = "This is the main body text of the email"
PS C:\> start-job -ScriptBlock { param ($body)
send-mailmessage -to "ITGroup <itdept@example.com>" -from "User01 <user01@example.com>" -subject "Demo email" -smtpServer smtp.example.com -Encoding UTF8 -UseSsl -Port 587} -ArgumentList $mailBody | Wait-Job | Recieve-Job

Send an HTML email:

 $body = "<HTML><HEAD><META http-equiv=""Content-Type"" content=""text/html; charset=iso-8859-1"" /><TITLE></TITLE></HEAD>"
 $body += "<BODY bgcolor=""#FFFFFF"" style=""font-size: Small; font-family: TAHOMA; color: #000000""><P>"
 $body += "Dear <b><font color=red>customer</b></font><br>"
 $body += "This is an <b>HTML</b> email<br>"
 $body += "Click <a href=https://www.google.com target=""_blank"">here</a> to open google <br>"
 send-mailmessage user01@example.com "Test email" -bodyashtml -Encoding UTF8 -body $body -from me@example.com -SmtpServer smtp.example.com -UseSsl -Port 587

“Learning music by reading about it is like making love by mail” ~ Luciano Pavarotti

Related PowerShell Cmdlets:

Out-Printer - Send the output to a printer


 
Copyright © 1999-2022 SS64.com
Some rights reserved