external help file | Locale | Module Name | ms.date | online version | schema | title |
---|---|---|---|---|---|---|
Microsoft.PowerShell.Commands.Utility.dll-Help.xml |
en-US |
Microsoft.PowerShell.Utility |
06/18/2024 |
2.0.0 |
Send-MailMessage |
Sends an email message.
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>] [-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-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.
Warning
The Send-MailMessage
cmdlet is obsolete. This cmdlet doesn't guarantee secure connections to
SMTP servers. While there is no immediate replacement available in PowerShell, we recommend you do
not use Send-MailMessage
. For more information, see
Platform Compatibility note DE0005.
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.
$sendMailMessageSplat = @{
From = 'User01 <user01@fabrikam.com>'
To = 'User02 <user02@fabrikam.com>'
Subject = 'Test mail'
}
Send-MailMessage @sendMailMessageSplat
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 isn't included.
This example sends an email message with an attachment.
$sendMailMessageSplat = @{
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'
}
Send-MailMessage @sendMailMessageSplat
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's 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
.
This example sends an email message to a mailing list.
$sendMailMessageSplat = @{
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 = $true
}
Send-MailMessage @sendMailMessageSplat
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's hidden from the other recipients. The Subject parameter is the
message because the optional Body parameter isn't 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.
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
.
Type: System.String[]
Parameter Sets: (All)
Aliases: PsPath
Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
Specifies the email addresses that receive a copy of the mail but aren't listed as recipients of the
message. Enter names (optional) and the email address, such as Name <someone@fabrikam.com>
.
Type: System.String[]
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Specifies the content of the email message.
Type: System.String
Parameter Sets: (All)
Aliases:
Required: False
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Specifies that the value of the Body parameter contains HTML.
Type: System.Management.Automation.SwitchParameter
Parameter Sets: (All)
Aliases: BAH
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
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>
.
Type: System.String[]
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
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?.
Type: System.Management.Automation.PSCredential
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: Current user
Accept pipeline input: False
Accept wildcard characters: False
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.
These values are defined as a flag-based enumeration. You can combine multiple values together to set multiple flags using this parameter. The values can be passed to the DeliveryNotification parameter as an array of values or as a comma-separated string of those values. The cmdlet will combine the values using a binary-OR operation. Passing values as an array is the simplest option and also allows you to use tab-completion on the values.
Type: System.Net.Mail.DeliveryNotificationOptions
Parameter Sets: (All)
Aliases: DNO
Accepted values: None, OnSuccess, OnFailure, Delay, Never
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Specifies the type of encoding for the target file. The default value is Default
.
The acceptable values for this parameter are as follows:
ASCII
Uses ASCII (7-bit) character set.BigEndianUnicode
Uses UTF-16 with the big-endian byte order.Default
Uses the encoding that corresponds to the system's active code page (usually ANSI).OEM
Uses the encoding that corresponds to the system's current OEM code page.Unicode
Uses UTF-16 with the little-endian byte order.UTF7
Uses UTF-7.UTF8
Uses UTF-8.UTF32
Uses UTF-32 with the little-endian byte order.
Type: System.Text.Encoding
Parameter Sets: (All)
Aliases: BE
Accepted values: ASCII, BigEndianUnicode, Default, OEM, Unicode, UTF7, UTF8, UTF32
Required: False
Position: Named
Default value: Default
Accept pipeline input: False
Accept wildcard characters: False
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>
.
Type: System.String
Parameter Sets: (All)
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
Specifies an alternate port on the SMTP server. The default value is 25, which is the default SMTP port.
Type: System.Int32
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: 25
Accept pipeline input: False
Accept wildcard characters: False
Specifies the priority of the email message. Normal is the default. The acceptable values for this parameter are Normal, High, and Low.
Type: System.Net.Mail.MailPriority
Parameter Sets: (All)
Aliases:
Accepted values: Normal, High, Low
Required: False
Position: Named
Default value: Normal
Accept pipeline input: False
Accept wildcard characters: False
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 isn't set and this parameter isn't used, the Send-MailMessage
command fails.
Type: System.String
Parameter Sets: (All)
Aliases: ComputerName
Required: False
Position: 3
Default value: $PSEmailServer
Accept pipeline input: False
Accept wildcard characters: False
The Subject parameter isn't required. This parameter specifies the subject of the email message.
Type: System.String
Parameter Sets: (All)
Aliases: sub
Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
The To parameter is required. This parameter specifies the recipient's email address. Enter
names (optional) and the email address, such as Name <someone@fabrikam.com>
.
Type: System.String[]
Parameter Sets: (All)
Aliases:
Required: True
Position: 0
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
The Secure Sockets Layer (SSL) protocol is used to establish a secure connection to the remote computer to send mail. By default, SSL isn't used.
Type: System.Management.Automation.SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
You can pipe the path and file names of attachments to this cmdlet.
This cmdlet returns no output.
The Send-MailMessage
cmdlet is obsolete. For more information, see
Platform Compatibility note DE0005. This cmdlet doesn't guarantee
secure connections to SMTP servers.
DE0005 suggests using the third-party library, MailKit. If you are using Exchange Online, you can use the Send-MgUserMail from the Microsoft Graph PowerShell SDK.