Tuesday 5 May 2020

Powershell Send-MailMessage

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


Send-MailMessage

Sends an email message.

Syntax

PowerShell
Send-MailMessage
    [-Attachments <String[]>]
    [-Bcc <String[]>]
    [[-Body] <String>]
    [-BodyAsHtml]
    [-Encoding <Encoding>]
    [-Cc <String[]>]
    [-DeliveryNotificationOption <DeliveryNotificationOptions>]
    -From <String>
    [[-SmtpServer] <String>]
    [-Priority <MailPriority>]
    [-ReplyTo <String[]>]
    [[-Subject] <String>]
    [-To] <String[]>
    [-Credential <PSCredential>]
    [-UseSsl]
    [-Port <Int32>]
    [<CommonParameters>]

Description

The Send-MailMessage cmdlet sends an email message from within PowerShell.
You must specify a Simple Mail Transfer Protocol (SMTP) server or the Send-MailMessage command 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. For more information, see about_Preference_Variables.

Examples

Example 1: Send an email from one person to another person
This example sends an email message from one person to another person.
The From, To, and Subject parameters are required by Send-MailMessage. This example uses the default $PSEmailServer variable for the SMTP server, so the SmtpServer parameter is not needed.
PowerShell
Send-MailMessage -From 'User01 <user01@fabrikam.com>' -To 'User02 <user02@fabrikam.com>' -Subject 'Test mail'
The Send-MailMessage cmdlet uses the From parameter to specify the message's sender. The To parameter specifies the message's recipient. The Subject parameter uses the text string Test mail as the message because the optional Body parameter is not included.
Example 2: Send an attachment
This example sends an email message with an attachment.
PowerShell
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'
The Send-MailMessage cmdlet uses the From parameter to specify the message's sender. The To parameter specifies the message's recipients. The Subject parameter describes the content of the message. The Body parameter is the content of the message.
The Attachments parameter specifies the file in the current directory that is attached to the email message. The Priority parameter sets the message to High priority. The -DeliveryNotificationOption parameter specifies two values, OnSuccess and OnFailure. The sender will receive email notifications to confirm the success or failure of the message delivery. The SmtpServer parameter sets the SMTP server to smtp.fabrikam.com.
Example 3: Send email to a mailing list
This example sends an email message to a mailing list.
PowerShell
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
The Send-MailMessage cmdlet uses the From parameter to specify the message's sender. The To parameter specifies the message's recipients. The Cc parameter sends a copy of the message to the specified recipient. The Bcc parameter sends a blind copy of the message. A blind copy is an email address that is hidden from the other recipients. The Subject parameter is the message because the optional Body parameter is not included.
The Credential parameter specifies a domain administrator's credentials are used to send the message. The UseSsl parameter specifies that Secure Socket Layer (SSL) creates a secure connection.

Parameters

-Attachments
Specifies the path and file names of files to be attached to the email message. You can use this parameter or pipe the paths and file names to Send-MailMessage.
TABLE 1
Type:String[]
Aliases:PsPath
Position:Named
Default value:None
Accept pipeline input:True (ByPropertyName, ByValue)
Accept wildcard characters:False
-Bcc
Specifies the 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@fabrikam.com>.
TABLE 2
Type:String[]
Position:Named
Default value:None
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False
-Body
Specifies the content of the email message.
TABLE 3
Type:String
Position:2
Default value:None
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False
-BodyAsHtml
Specifies that the value of the Body parameter contains HTML.
TABLE 4
Type:SwitchParameter
Aliases:BAH
Position:Named
Default value:None
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False
-Cc
Specifies the 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@fabrikam.com>.
TABLE 5
Type:String[]
Position:Named
Default value:None
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False
-Credential
Specifies a user account that has permission to perform this action. The default is the current user.
Type a user name, such as User01 or Domain01\User01. Or, enter a PSCredential object, such as one from the Get-Credential cmdlet.
Credentials are stored in a PSCredential object and the password is stored as a SecureString.
 Note
For more information about SecureString data protection, see How secure is SecureString?.
TABLE 6
Type:PSCredential
Position:Named
Default value:Current user
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False
-DeliveryNotificationOption
Specifies the delivery notification options for the email message. You can specify multiple values. None is the default value. The alias for this parameter is DNO.
The delivery notifications are sent to the address in the From parameter.
The acceptable values for this parameter are as follows:
  • 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.
TABLE 7
Type:DeliveryNotificationOptions
Aliases:DNO
Accepted values:None, OnSuccess, OnFailure, Delay, Never
Position:Named
Default value:None
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False
-Encoding
Specifies the type of encoding for the target file. The default value is UTF8NoBOM.
The acceptable values for this parameter are as follows:
  • ASCII: Uses the encoding for the ASCII (7-bit) character set.
  • BigEndianUnicode: Encodes in UTF-16 format using the big-endian byte order.
  • OEM: Uses the default encoding for MS-DOS and console programs.
  • Unicode: Encodes in UTF-16 format using the little-endian byte order.
  • UTF7: Encodes in UTF-7 format.
  • UTF8: Encodes in UTF-8 format.
  • UTF8BOM: Encodes in UTF-8 format with Byte Order Mark (BOM)
  • UTF8NoBOM: Encodes in UTF-8 format without Byte Order Mark (BOM)
  • UTF32: Encodes in UTF-32 format.
Beginning with PowerShell 6.2, the Encoding parameter also allows numeric IDs of registered code pages (like -Encoding 1251) or string names of registered code pages (like -Encoding "windows-1251"). For more information, see the .NET documentation for Encoding.CodePage.
TABLE 8
Type:Encoding
Aliases:BE
Accepted values:ASCII, BigEndianUnicode, OEM, Unicode, UTF7, UTF8, UTF8BOM, UTF8NoBOM, UTF32
Position:Named
Default value:UTF8NoBOM
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False
-From
The From parameter is required. This parameter specifies the sender's email address. Enter a name (optional) and email address, such as Name <someone@fabrikam.com>.
TABLE 9
Type:String
Position:Named
Default value:None
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False
-Port
Specifies an alternate port on the SMTP server. The default value is 25, which is the default SMTP port.
TABLE 10
Type:Int32
Position:Named
Default value:25
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False
-Priority
Specifies the priority of the email message. Normal is the default. The acceptable values for this parameter are Normal, High, and Low.
TABLE 11
Type:MailPriority
Accepted values:Normal, High, Low
Position:Named
Default value:Normal
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False
-ReplyTo
Specifies additional email addresses (other than the From address) to use to reply to this message. Enter names (optional) and the email address, such as Name <someone@fabrikam.com>.
This parameter was introduced in PowerShell 6.2.
TABLE 12
Type:String[]
Position:Named
Default value:None
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False
-SmtpServer
Specifies 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 not used, the Send-MailMessage command fails.
TABLE 13
Type:String
Aliases:ComputerName
Position:3
Default value:$PSEmailServer
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False
-Subject
The Subject parameter isn't required. This parameter specifies the subject of the email message.
TABLE 14
Type:String[]
Aliases:sub
Position:1
Default value:None
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False
-To
The To parameter is required. This parameter specifies the recipient's email address. If there are multiple recipients, separate their addresses with a comma (,). Enter names (optional) and the email address, such as Name <someone@fabrikam.com>.
TABLE 15
Type:String[]
Position:0
Default value:None
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False
-UseSsl
The Secure Sockets Layer (SSL) protocol is used to establish a secure connection to the remote computer to send mail. By default, SSL is not used.
TABLE 16
Type:SwitchParameter
Position:Named
Default value:None
Accept pipeline input:True (ByPropertyName)
Accept wildcard characters:False

Inputs

You can pipe the path and file names of attachments to Send-MailMessage.

Outputs

None
This cmdlet does not generate any output.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Blog Archive