如何使用 PowerShell 發送電子郵件

Jacki

通過 PowerShell 發送電子郵件時,我們熟悉 Send-MailMessage cmdlet。不幸的是,Send-MailMessage cmdlet 已過時,不建議再使用。那麼,Send-MailMessage 的替代方案是什麼?答案是Send-MailKitMessage。在本文中,您將了解如何使用 PowerShell 發送電子郵件。

Send-MailMessage cmdlet 已過時

發送郵件消息cmdlet 很流行通過 PowerShell 發送電子郵件。不幸的是,Send-MailMessage cmdlet 已過時,您不應再使用它。

筆記:Send-MailMessage cmdlet 已過時。此 cmdlet 不保證與 SMTP 服務器的安全連接。雖然 PowerShell 中沒有立即可用的替代方案,但我們建議您不要使用 Send-MailMessage。

既然默認的 PowerShell cmdlet 中沒有可用的替代品,那麼我們是否可以安裝並使用一個模塊來從 PowerShell 發送電子郵件?是的,有。這發送MailKitMessage模塊是 Send-MailMessage cmdlet 的絕佳替代品。

配置 SMTP 中繼

如果您想設置 SMTP 中繼,請閱讀以下文章:

  • 在 Exchange Server 中配置匿名 SMTP 中繼
  • 配置 Office 365 SMTP 中繼

安裝Send-MailKitMessage模塊

要在您的系統上安裝 Send-MailKitMessage 模塊,請執行以下步驟。

1. 設置 Windows PowerShell 執行策略

默認情況下,我們無法安裝腳本。要要求從 Internet 下載的所有 PowerShell 腳本均由受信任的發布者簽名,請以管理員身份運行 PowerShell,然後運行 ​​cmdlet。

Set-ExecutionPolicy RemoteSigned

重要的:關閉並重新打開提升的 Windows PowerShell 窗口以使更改生效。

2.安裝PowerShellGet模塊

以管理員身份運行 PowerShell。運行命令安裝模塊 PowershellGet -Force。當要求安裝 NuGet 提供程序時,請按並跟隨進入

Install-Module PowershellGet -Force

如果出現無法安裝的錯誤,請閱讀文章無法安裝 PowerShell 的 NuGet 提供程序。

3.安裝Send-MailKitMessage模塊

安裝Send-MailKitMessage 模塊

Install-Module -Name Send-MailKitMessage -Force

現在模塊已安裝,讓我們看一些如何使用 PowerShell 發送電子郵件的示例。

Send-MailKitMessage 參數

使用 Send-MailKitMessage 模塊時可以使用以下參數。

# Use secure connection if available ([bool], optional)
$UseSecureConnectionIfAvailable = $true

# Authentication ([System.Management.Automation.PSCredential], optional)
$Credential = [System.Management.Automation.PSCredential]::new("Username", (Read-Host -Prompt "Enter password" -AsSecureString))

# SMTP server ([string], required)
$SMTPServer = "SMTPServer"

# Port ([int], required)
$Port = "PortNumber"

# Sender ([MimeKit.MailboxAddress] https://www.mimekit.net/docs/html/T_MimeKit_MailboxAddress.htm, required)
$From = [MimeKit.MailboxAddress]"SenderEmailAddress"

# Recipient list ([MimeKit.InternetAddressList] https://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, required)
$RecipientList = [MimeKit.InternetAddressList]::new()
$RecipientList.Add([MimeKit.InternetAddress]"Recipient1EmailAddress")

# CC list ([MimeKit.InternetAddressList] https://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, optional)
$CCList = [MimeKit.InternetAddressList]::new()
$CCList.Add([MimeKit.InternetAddress]"CCRecipient1EmailAddress")

# BCC list ([MimeKit.InternetAddressList] https://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, optional)
$BCCList = [MimeKit.InternetAddressList]::new()
$BCCList.Add([MimeKit.InternetAddress]"BCCRecipient1EmailAddress")

# Subject ([string], optional)
$Subject = [string]"Subject"

# Text body ([string], optional)
$TextBody = [string]"TextBody"

# HTML body ([string], optional)
$HTMLBody = [string]"HTMLBody"

# Attachment list ([System.Collections.Generic.List[string]], optional)
$AttachmentList = [System.Collections.Generic.List[string]]::new()
$AttachmentList.Add("Attachment1FilePath")

# Splat parameters
$Parameters = @{
    "UseSecureConnectionIfAvailable" = $UseSecureConnectionIfAvailable    
    "Credential"                     = $Credential
    "SMTPServer"                     = $SMTPServer
    "Port"                           = $Port
    "From"                           = $From
    "RecipientList"                  = $RecipientList
    "CCList"                         = $CCList
    "BCCList"                        = $BCCList
    "Subject"                        = $Subject
    "TextBody"                       = $TextBody
    "HTMLBody"                       = $HTMLBody
    "AttachmentList"                 = $AttachmentList
}

# Send message
Send-MailKitMessage @Parameters

以下是一些可用於通過 PowerShell 發送電子郵件的模板。

需要以下參數:

  • SMTP服務器
  • 港口
  • 收件人列表

發送電子郵件給收件人

此示例將電子郵件從發件人發送到收件人。

參見:使用 Microsoft Graph API 和 PowerShell 發送電子郵件

# SMTP server ([string], required)
$SMTPServer = "mail.exoip.com"

# Port ([int], required)
$Port = "25"

# Sender ([MimeKit.MailboxAddress] https://www.mimekit.net/docs/html/T_MimeKit_MailboxAddress.htm, required)
$From = [MimeKit.MailboxAddress]"[email protected]"

# Recipient list ([MimeKit.InternetAddressList] https://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, required)
$RecipientList = [MimeKit.InternetAddressList]::new()
$RecipientList.Add([MimeKit.InternetAddress]"[email protected]")

# Subject ([string], optional)
$Subject = [string]"This is the subject"

# Text body ([string], optional)
$TextBody = [string]"This is the text body"

# Splat parameters
$Parameters = @{
    "SMTPServer"    = $SMTPServer
    "Port"          = $Port
    "From"          = $From
    "RecipientList" = $RecipientList
    "Subject"       = $Subject
    "TextBody"      = $TextBody
}

# Send message
Send-MailKitMessage @Parameters

這是它在收件人收件箱中的外觀。

發送帶有附件的電子郵件

此示例將一封帶有附件的電子郵件發送給多個收件人。

# SMTP server ([string], required)
$SMTPServer = "mail.exoip.com"

# Port ([int], required)
$Port = "25"

# Sender ([MimeKit.MailboxAddress] https://www.mimekit.net/docs/html/T_MimeKit_MailboxAddress.htm, required)
$From = [MimeKit.MailboxAddress]"[email protected]"

# Recipient list ([MimeKit.InternetAddressList] https://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, required)
$RecipientList = [MimeKit.InternetAddressList]::new()
$RecipientList.Add([MimeKit.InternetAddress]"[email protected]")
$RecipientList.Add([MimeKit.InternetAddress]"[email protected]")

# Subject ([string], optional)
$Subject = [string]"Sending the Attachment"

# Text body ([string], optional)
$TextBody = [string]"See the CSV file attachment."

# Attachment list ([System.Collections.Generic.List[string]], optional)
$AttachmentList = [System.Collections.Generic.List[string]]::new()
$AttachmentList.Add("C:tempFile.csv")

# Splat parameters
$Parameters = @{
    "SMTPServer"     = $SMTPServer
    "Port"           = $Port
    "From"           = $From
    "RecipientList"  = $RecipientList
    "Subject"        = $Subject
    "TextBody"       = $TextBody
    "AttachmentList" = $AttachmentList
}

# Send message
Send-MailKitMessage @Parameters

這是它在收件人收件箱中的外觀。

發送電子郵件到郵件列表

此示例向郵件列表發送一封電子郵件,其中抄送和密件抄送字段中包含其他收件人。

# SMTP server ([string], required)
$SMTPServer = "mail.exoip.com"

# Port ([int], required)
$Port = "25"

# Sender ([MimeKit.MailboxAddress] https://www.mimekit.net/docs/html/T_MimeKit_MailboxAddress.htm, required)
$From = [MimeKit.MailboxAddress]"[email protected]"

# Recipient list ([MimeKit.InternetAddressList] https://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, required)
$RecipientList = [MimeKit.InternetAddressList]::new()
$RecipientList.Add([MimeKit.InternetAddress]"[email protected]")

# CC list ([MimeKit.InternetAddressList] https://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, optional)
$CCList = [MimeKit.InternetAddressList]::new()
$CCList.Add([MimeKit.InternetAddress]"[email protected]")

# BCC list ([MimeKit.InternetAddressList] https://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, optional)
$BCCList = [MimeKit.InternetAddressList]::new()
$BCCList.Add([MimeKit.InternetAddress]"[email protected]")

# Subject ([string], optional)
$Subject = [string]"To all the members"

# Text body ([string], optional)
$TextBody = [string]"Check out the magic."

# Splat parameters
$Parameters = @{
    "SMTPServer"     = $SMTPServer
    "Port"           = $Port
    "From"           = $From
    "RecipientList"  = $RecipientList
    "CCList"         = $CCList
    "BCCList"        = $BCCList
    "Subject"        = $Subject
    "TextBody"       = $TextBody
}

# Send message
Send-MailKitMessage @Parameters

這是它在收件人收件箱中的外觀。

通過安全連接 (TLS) 發送電子郵件

傳輸層安全 (TLS) 是一種加密電子郵件以確保安全和隱私的協議。

此示例從發件人發送一封電子郵件,該發件人需要通過安全連接 (TLS) 向收件人進行身份驗證。

# Use secure connection if available ([bool], optional)
$UseSecureConnectionIfAvailable = $true

# Authentication ([System.Management.Automation.PSCredential], optional)
$Credential = [System.Management.Automation.PSCredential]::new("[email protected]", (Read-Host -Prompt "Enter password" -AsSecureString))

# SMTP server ([string], required)
$SMTPServer = "mail.exoip.com"

# Port ([int], required)
$Port = "587"

# Sender ([MimeKit.MailboxAddress] https://www.mimekit.net/docs/html/T_MimeKit_MailboxAddress.htm, required)
$From = [MimeKit.MailboxAddress]"[email protected]"

# Recipient list ([MimeKit.InternetAddressList] https://www.mimekit.net/docs/html/T_MimeKit_InternetAddressList.htm, required)
$RecipientList = [MimeKit.InternetAddressList]::new()
$RecipientList.Add([MimeKit.InternetAddress]"[email protected]")

# Subject ([string], optional)
$Subject = [string]"Sending with Port 587"

# Text body ([string], optional)
$TextBody = [string]"Sending the message on Port 587 (TLS)"

# Splat parameters
$Parameters = @{
    "UseSecureConnectionIfAvailable" = $UseSecureConnectionIfAvailable
    "Credential"                     = $Credential
    "SMTPServer"                     = $SMTPServer
    "Port"                           = $Port
    "From"                           = $From
    "RecipientList"                  = $RecipientList
    "Subject"                        = $Subject
    "TextBody"                       = $TextBody
}

# Send message
Send-MailKitMessage @Parameters

就是這樣!

結論

您學習瞭如何使用 Send-MailKitMessage 模塊通過 PowerShell 發送電子郵件。它是 Send-MailMessage cmdlet 的絕佳替代品,後者已不再使用。安裝該模塊並調整腳本,或使用它來測試外發 SMTP 郵件流。

您喜歡這篇文章嗎?您可能還喜歡如何將 Microsoft 365 配置為僅接受來自第三方垃圾郵件篩選器的郵件。不要忘記關注我們並分享這篇文章。