Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Passing the content of txt file to body of Send-MailMessage

$
0
0

Hi,

I am trying to send a report using send-mailmessage cmdlet and I am unable to pass the content of the file as it is to body of the message. It gets converted to a single line string (the escape character aren't getting recognized) or body of the mail has system.object[] as output.

Script1:

$body = Get-Content -Path C:\TEMP\Body.txt | Out-String
Send-MailMessage -To <toaddress> -Cc <ccaddress> -From <fromaddress> -Subject "$subject" -Body [string]$body -BodyAsHtml -SmtpServer $server

Error:
Body of the mail is seen as [string]<content in a continous line>. The actual body in the txt doc is written as we generally write emails

Script2:

$body = Get-Content -Path C:\TEMP\Body.txt
Send-MailMessage -To <toaddress> -Cc <ccaddress> -From <fromaddress> -Subject "$subject" -Body $body -BodyAsHtml -SmtpServer $server

Error:
Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.
At line:1 char:149
+ ... subject" -Body $body -BodyAsHtml -SmtpServer $server
+                    ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage

Script3:

$body = [string]::Join("`r`n",(Get-Content -Path C:\TEMP\Body.txt))
Send-MailMessage -To <toaddress> -Cc <ccaddress> -From <fromaddress> -Subject "$subject" -Body $body -BodyAsHtml -SmtpServer $server

Error:
Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.
At line:1 char:149
+ ... subject" -Body $body -BodyAsHtml -SmtpServer $server
+                    ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage

Thanks in Advance,
Ankush Shah



Viewing all articles
Browse latest Browse all 15028

Trending Articles