0

I am trying to send an email with the PowerShell function Send-MailMessage, and I am in front of various issues that are quite strange :

  • First of all, the -To parameter is not proposed when I write this function in the PowerShell ISE. And if I try to add it anyway, if have the following error Send-MailMessage : A positional parameter cannot be found that accepts argument 'True'.
  • So to bypass this problem, I found that I could send an email to someone using the -Cc parameter. So my call to Send-MailMessage looks like the following

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"

The problem is that when this line is executing, it's raising an error :

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

But the most strange here is that the mail has been send to the personn specified in -Cc... I don't understand why I can't add this -To argument, and most of all why it is sending the message even if there is an error...

Any idea ?

3
  • What is the full value of $recipient? Commented Jul 27, 2017 at 13:22
  • I can't publish the real value, but it's exactly like "[email protected]" Commented Jul 27, 2017 at 13:25
  • Not sure if it's the only problem, but -BodyAsHtml $true is (afaik) wrong, it should either be only -BodyAsHtml or -BodyAsHtml:$true Commented Jul 27, 2017 at 13:38

1 Answer 1

3

If you check the syntax

Get-Command Send-MailMessage -Syntax

the parameter [-BodyAsHtml] is you add is type of switch and not expecting $true value. You can specify -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>]

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that was perfectly what I did wrong. I'll be more carefull with the syntax now. Thanks a lot !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.