如何使用 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 发送电子邮件的模板。

建议阅读:如何在 iPhone 上使用 S/MIME 或 Gmail 发送加密电子邮件

需要以下参数:

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

发送电子邮件给收件人

此示例将电子邮件从发件人发送到收件人。

# 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 配置为仅接受来自第三方垃圾邮件筛选器的邮件。不要忘记关注我们并分享这篇文章。