2017-07-27 165 views
0

我试图用PowerShell的功能发送-MAILMESSAGE发送电子邮件,和我在的各种问题面前是很奇怪:PowerShell的电子邮件错误发送

  • 所有的第一,当我在PowerShell ISE中编写此函数时,不会提出参数- 至。如果我尝试添加也无妨,如果有以下错误 Send-MailMessage : A positional parameter cannot be found that accepts argument 'True'.
  • 所以绕过这个问题,我发现我可以使用-Cc参数发送电子邮件给某人。所以我到发送-MAILMESSAGE调用看起来像下面

Send-MailMessage -Body $body -BodyAsHtml $true -From "[email protected]" -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中指定的人员...我不明白为什么我不能添加这个- 到的说法,最重要的是为什么它发送的消息,即使有错误...

任何想法?

+0

什么是'$ recipient'的全部价值? – gravity

+0

我无法发布实际价值,但它完全像“[email protected]” –

+0

不确定它是唯一的问题,但是'-BodyAsHtml $ true'(afaik)错误,它应该只是'-BodyAsHtml'或'-BodyAsHtml:$ true' – whatever

回答

3

如果您检查语法

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

+0

谢谢,那完全是我做错了。现在我会更加小心语法。非常感谢 ! –