简体   繁体   中英

Powershell email sending with an error

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 "senderadress@safrangroup.com" -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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM