使用 PowerShell Cmdlet Send-MailMessage 輕鬆發送郵件

對於今天的 PowerShell 命令,我們將介紹 PowerShell 命令 Send-MailMessage。這個 PowerShell 命令只有一個目的,就是以各種不同的方式發送電子郵件。

由於 Send-MailMessage 命令有很多發送電子郵件的方式,讓我們馬上開始介紹所有常見的範例。在本文中,我們將涵蓋從最簡單到一些我不希望任何人遇到的情境的多個範例。

這篇文章將是一個很好的資源,以防您遇到需要使用 PowerShell 發送電子郵件並忘記語法的情況。

SMTP 伺服器

所有的電子郵件都必須通過某個 SMTP 伺服器以及一個伺服器和一個連接埠進行傳送。要指定要使用的 SMTP 伺服器,使用一個名為 SMTPServer 的參數,該參數允許您指定要建立連接並通過其中繼郵件的 SMTP 伺服器。但這不是必需的。

如果您沒有為 SMTPServer 參數指定值,則將使用存儲在 $PSEmailServer 偏好變數中的值。您可以像其他變數一樣將值分配給此變數。

Assigning a value to the $PSEmailServer preference variable
PS51> $PSEmailServer = 'smtp.server.local'

然而,這個變數在不同的PowerShell會話中無法保持。這意味著每次打開新的PowerShell會話時,您都需要重新定義它。基於這個原因,我建議在您的Send-MailMessage參考之上定義它,或者不定義它。相反,我個人建議提供SMTPServer參數值。這樣您就不需要管理一個可能會更改的外部變數。

PS51> Send-MailMessage -SmtpServer 'smtp.server.local'

端口

默認情況下,該命令將嘗試通過SMTP服務器發送郵件的端口是25。這是普通的,未加密的SMTP。然而,現在更常見的是使用SSL/TLS加密發送電子郵件(我們稍後將介紹這些情況)。

如果您需要更改端口從25開始,可以使用Port參數。

PS51> Send-MailMessage -SmtpServer 'smtp.server.local' -Port 587

收件人和發件人

通過像ToCcBcc這樣的方法向不同的收件人發送郵件,該命令提供了各種參數來適應這一需求。

Send-Mailmessage命令中的To、Cc和Bcc參數

該命令具有三個參數,每個參數都支持使用逗號分隔的多個收件人,分別為ToCcBcc

您可以像下面這樣指定單個地址。

Sending email to one recipient via To, Cc and Bcc
PS51> Send-MailMessage -To joe@gmail.com -Cc bob@gmail.com -Bcc susie@hotmail.com -Subject 'this is a subject'

或者您可以為每個參數值指定多個收件人。

Sending email to multiple recipients via To, Cc and Bcc
PS51> Send-MailMessage -To joe@gmail.com, tony@mycompany.local -Cc bob@gmail.com, rick@othercompany.com -Bcc susie@hotmail.com,secret@fooorg.org -Subject 'this is a subject'

發件人參數

在發送電子郵件時,您還可以指定From參數,這將設置電子郵件中的回覆標頭。此參數僅允許一個地址。如果收件人選擇回覆郵件,回覆將發送到這個電子郵件地址。

Sending email from an address
PS51> Send-MailMessage -From me@company.org -To joe@gmail.com -Subject 'this is a subject'

默認情況下,使用電子郵件地址只會在收件人的FROM欄位中顯示該電子郵件地址。但是,如果您想指定一個名稱或類似服務帳戶郵箱的標籤,您還可以指定一個名稱和一個電子郵件地址,如下所示:

Adam Bertram <adbertram@gmail.com>

請注意,使用了Subject參數。此參數始終是必需的。您將在所有示例中看到此參數的使用。

正文

Body參數允許您指定電子郵件正文的內容。最簡單的情況下,您可以指定任何文本,命令碼將以純文本形式發送它。

PS51> Send-MailMessage -From me@company.org -To joe@gmail.com -Subject 'this is a subject' -Body 'this is the body'

發送HTML正文郵件

您還可以通過HTML而不是純文本發送電子郵件正文。為此,使用與純文本相同的Body參數,但將HTML用於字符串,並使用BodyAsHtml切換參數。

Sending an HTML table in an email
$body = @' <table style="width:100%"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> </table> '@ PS51> Send-MailMessage -From me@company.org -To joe@gmail.com -Subject 'this is a subject' -Body $body -BodyAsHtml

注意,在上面的示例中,我将正文字符串括在@''@中。这被称为here string,它允许您定义长字符串,通常在包含回车的电子邮件正文中使用。Here string保留字符串的格式,并且是定义电子邮件正文的好方法,特别是在HTML中。

编码

如果电子邮件的主题或正文中包含特殊字符,您可以使用Encoding参数。该参数允许您在发送之前通过指定的编码类型对电子邮件主题和正文进行编码。

您在这里有几个选项:

  • ASCII(默认)
  • UTF8
  • UTF7
  • UTF32
  • Unicode
  • BigEndianUnicode
  • Default
  • OEM
PS51> Send-MailMessage -From me@company.org -To joe@gmail.com -Subject 'this is a subject' -Body 'this is the body' -Encoding UTF8

附件

此命令还可以附加一个或多个文件。为此,您可以使用Attachments参数,并提供要附加的文件的路径。

Attaching the C:\file.doc to an email
PS51> Send-MailMessage -From me@company.org -To joe@gmail.com -Subject 'this is a subject' -Body 'this is the body' -Attachments 'C:\file.doc'

您还可以通过使用逗号将它们分开,通过集合指定多个附件。

Attaching the C:\file.doc and D:\report.xlsx file to an email
PS51> Send-MailMessage -From me@company.org -To joe@gmail.com -Subject 'this is a subject' -Body 'this is the body' -Attachments 'C:\file.doc','D:\report.xlsx'

Attachments参数还允许您通过像Get-ItemGet-ChildItem这样的命令将文件传输到Send-MailMessage命令中。

PS51> Get-ChildItem -Path 'C:\MyFiles' | Send-MailMessage -From me@company.org -To joe@gmail.com -Subject 'this is a subject' -Body 'this is the body'

安全和验证的电子邮件

默認情況下,這個 cmdlet 通過未加密的 SMTP 通信在 25 號端口上發送電子郵件。然而,它也支持通過 SSL/TLS 加密的方式發送電子郵件,需要提供用戶名和密碼。

如果您嘗試通過需要身份驗證的 SMTP 服務器中繼郵件,該命令將失敗並顯示以下錯誤消息。

Error message when attempting to send unencrypted email.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first.

要解決此問題,您首先需要指定端口(通常是 587,用於 TLS)和 UseSsl 參數。這告訴 cmdlet 嘗試連接到 SMTP 服務器的 587 端口並加密整個消息。通常(總是?)還需要使用 Credential 參數指定用於身份驗證到 SMTP 服務器的用戶名/密碼。

PS51> $credential = Get-Credential
PS51> Send-MailMessage -From me@company.org -To joe@gmail.com -Subject 'this is a subject' -Body 'this is the body' -UseSsl -Port 587 -Credential $credential

上面的代碼使用 Get-Credential cmdlet 獲取一個憑據(PSCredential)對象。這有時會引起問題,因為它是交互式的,意味著它會停止腳本並要求輸入用戶名和密碼。為了防止這種情況發生,您可以即時創建一個 PSCredential 對象

A common email example is to use Gmail. Using the knowledge you’ve gained above, you can now easily send email through Gmail using the smtp.gmail.com SMTP server as shown below.

$gmailCred = Get-Credential

$sendMailParams = @{
    From = '[email protected]' ## 必須是 gmail.com
    To = '[email protected]'
    Subject = 'some subject'
    Body = 'some body'
    SMTPServer = 'smtp.gmail.com'
    SMTPPort = 587
    UseSsl = $true
    Credential = $gmailCred
}

Send-MailMessage @sendMailParams

指定郵件優先級

雖然我個人希望郵件的這個特性能消失,但您可以通過 Priority 參數為發送的郵件指定優先級。這個優先級會被電子郵件客戶端以不同的方式解釋。

High priority message in Outlook

Send-mailmessage 允許您指定三個不同的郵件優先級。

  • 正常(默認)
Assigning a high-priority email
PS51> Send-MailMessage -From me@company.org -To joe@gmail.com -Subject 'this is a subject' -Body 'this is the body' -Priority High

但是拜託,求求你們不要以為你們所有的電子郵件都是高優先級的!

傳送通知

最後,您可以為電子郵件指定傳送通知。傳送通知通常在某些電子郵件客戶端中被稱為已讀回執。傳送通知可讓您在電子郵件被收件人接收時得到通知。然而,收件人仍然需要允許此功能。

在請求傳送通知時,您有四個選項。

  • 無(預設)
  • 成功時(當電子郵件成功傳送時)
  • 失敗時(通知傳送失敗)
  • 延遲時(當電子郵件經由SMTP服務器延遲時)

您可以通過使用DeliveryNotificationOptions參數來指定傳送通知選項。

Requesting a notification on delivery
PS51> Send-MailMessage -From me@company.org -To joe@gmail.com -Subject 'this is a subject' -Body 'this is the body' -DeliveryNotificationsOptions 'OnSuccess'

您還可以通過用逗號分隔它們一次請求多個傳送通知。

Requesting a notification for all types
PS51> Send-MailMessage -From me@company.org -To joe@gmail.com -Subject 'this is a subject' -Body 'this is the body' -DeliveryNotificationsOptions 'OnSuccess','OnFailure','Delay'

總結

在本文中,您了解了Send-mailmessage cmdlet以及它的所有參數和示例。我希望本文可以在您使用Send-mailmessage cmdlet時作為參考。

進一步閱讀

請務必查看其他相關文章!

Source:
https://adamtheautomator.com/send-mailmessage/