Send-MailMessage

Sends an email message.

语法

All

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.

示例

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.

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.

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.

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.

参数

-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.

参数属性

类型:

String[]

默认值:None
支持通配符:False
不显示:False
别名:PsPath

参数集

(All)
Position:Named
必需:False
来自管道的值:True
来自管道的值(按属性名称):False
来自剩余参数的值: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>.

参数属性

类型:

String[]

默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Body

Specifies the content of the email message.

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:2
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-BodyAsHtml

Specifies that the value of the Body parameter contains HTML.

参数属性

类型:SwitchParameter
默认值:None
支持通配符:False
不显示:False
别名:BAH

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值: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>.

参数属性

类型:

String[]

默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值: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.

参数属性

类型:PSCredential
默认值:Current user
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值: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.

参数属性

类型:DeliveryNotificationOptions
默认值:None
接受的值:None, OnSuccess, OnFailure, Delay, Never
支持通配符:False
不显示:False
别名:DNO

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Encoding

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.

参数属性

类型:Encoding
默认值:Default
接受的值:ASCII, BigEndianUnicode, Default, OEM, Unicode, UTF7, UTF8, UTF32
支持通配符:False
不显示:False
别名:BE

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值: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>.

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:True
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Port

Specifies an alternate port on the SMTP server. The default value is 25, which is the default SMTP port.

参数属性

类型:Int32
默认值:25
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Priority

Specifies the priority of the email message. Normal is the default. The acceptable values for this parameter are Normal, High, and Low.

参数属性

类型:MailPriority
默认值:Normal
接受的值:Normal, High, Low
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值: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.

参数属性

类型:String
默认值:$PSEmailServer
支持通配符:False
不显示:False
别名:ComputerName

参数集

(All)
Position:3
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Subject

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

参数属性

类型:String
默认值:None
支持通配符:False
不显示:False
别名:sub

参数集

(All)
Position:1
必需:True
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值: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>.

参数属性

类型:

String[]

默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:0
必需:True
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值: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.

参数属性

类型:SwitchParameter
默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

输入

String

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

输出

None

This cmdlet does not generate any output.