【问题标题】:Powershell email sending with an errorPowershell 电子邮件发送错误
【发布时间】:2017-07-27 13:20:27
【问题描述】:

我正在尝试使用 PowerShell 函数 Send-MailMessage 发送电子邮件,但我遇到了各种非常奇怪的问题:

  • 首先,我在PowerShell ISE中写这个函数的时候没有提出-To参数。如果我尝试添加它,如果出现以下错误 Send-MailMessage : A positional parameter cannot be found that accepts argument 'True'.
  • 为了绕过这个问题,我发现我可以使用 -Cc 参数向某人发送电子邮件。所以我对 Send-MailMessage 的调用如下所示

Send-MailMessage -Body $body -BodyAsHtml $true -From "senderadress@safrangroup.com" -Cc $recipient -Priority High - Encoding ([System.Text.Encoding]::UTF8) -SmtpServer "my.smtp.server" -Subject ("My subject") -Attachments "RESSOURCES/logo.png"

问题是当这条线执行时,它会引发一个错误:

Send-MailMessage : The specified string is not in the form required for an e-mail address.

但是这里最奇怪的是邮件已经发送到-Cc中指定的人了……我不明白为什么我不能添加这个-To 参数,最重要的是为什么即使有错误它也会发送消息......

有什么想法吗?

【问题讨论】:

  • $recipient 的完整值是多少?
  • 我不能公布真正的价值,但它和“name.subname@group.com”一模一样
  • 不确定这是否是唯一的问题,但-BodyAsHtml $true 是(afaik)错误,它应该只是-BodyAsHtml-BodyAsHtml:$true

标签: powershell email


【解决方案1】:

如果你检查语法

Get-Command Send-MailMessage -Syntax

您添加的参数[-BodyAsHtml] 是开关类型,而不是$true 值。你可以指定-BodyAsHtml:$true

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

【讨论】:

  • 谢谢,这完全是我做错的。我现在会更加小心语法。非常感谢!
猜你喜欢
  • 2015-12-24
  • 1970-01-01
  • 2010-09-25
  • 1970-01-01
  • 2015-03-17
  • 2016-05-06
  • 2016-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多