none
Send-MailMessage cmdlet not allowing me to set -encoding RRS feed

  • Question

  • When I try to set -encoding with this cmdlet it returns an error:

    sample code:

    send-mailmessage -from

     

    "address@mydomain.com" -to "address@mydomain.com" -subject "Test" -body "Testing" -smtpServer aserver@mydomain.com -Encoding ASCII

    error message:

     

     

     

    Cannot bind parameter 'Encoding'. Cannot convert value "ASCII" to type "System.Text.Encoding". Error: "Invalid cast from 'System.String' to 'System.Text.Encoding'."

    At :line:2 char:155

    + send-mailmessage -from "address@mydomain.com" -to "address@mydomain.com" -subject "Test" -body "Testing" -smtpServer aserver@mydomain.com -Encoding <<<< ASCII

    The default is supposed to be in ASCII I want to force it to be sure, but the -encoding switch isnt working no matter what type of encoding I try to set.  Any thoughts?

     

    • Edited by Rumpled166 Friday, May 29, 2009 6:58 AM
    Friday, May 29, 2009 6:47 AM

Answers

All replies

  • I supposed you're trying out v2.

    PS> help send-mailmessage -full

    Seems -encoding requires a special type of object, possibly defined here:
    http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx

    Sorry, I don't have time right now to look into this, but it doesn't appear that you can just do something simple like doing:
    ... -encoding "ascii" ...

    You're going to need to create a System.Text.Encoding object and pass that as the value to the parameter.

    • Marked as answer by Mervyn Zhang Thursday, June 4, 2009 7:59 AM
    Friday, May 29, 2009 11:47 AM
    Moderator
  • Looks like a bug to me, at the meantime try to specify encoding like so:

    PS > send-mailmessage -from "address@mydomain.com" -to "address@mydomain.com" -subject "Test" -body "Testing" -smtpServer aserver@mydomain.com -Encoding ([System.Text.Encoding]::ASCII)


    You can also follow Marco's suggestion:

    $encoding = [System.Text.Encoding]::ASCII

    PS > send-mailmessage -from "address@mydomain.com" -to "address@mydomain.com" -subject "Test" -body "Testing" -smtpServer aserver@mydomain.com -Encoding $encoding

     By the way, Encoding's default value is: ASCII.

     

    EDIT: it is probably not a bug. [System.Text.Encoding] is not an enum, and ASCII is not an enumerated value. It's a static property of the System.Text.Encoding type, of type System.Text.Encoding. I would file a suggestion on MS connect site.
    Shay Levy [MVP]
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar
    Saturday, May 30, 2009 6:22 PM
    Moderator
  • Kinda late perhaps, but I struggled with this too and found that using this (in my case UTF8) worked:

    $enc  = New-Object System.Text.utf8encoding

    Send-MailMessage -to "Firstname Lastname <Firstname.Lastname@domain.com>" -From "Firstname2 Lastname2 <Firstname2.Lastname2@domain.com>" `
    -Subject "Test mail from PowerShell" -Body "Sending mail trough PowerShell V2 Send-MailMessage cmdlet" `
    -SmtpServer mailserver.domain.com -Encoding $enc

    Be aware of the "new line" chars, when pasting ;)


    /A
    • Proposed as answer by 20190305 Friday, July 26, 2019 12:37 PM
    Thursday, March 31, 2011 8:23 AM
  • Thanks for this, it helped me a bit. :)

    But can you maybe explain something strange.

    On a Windows Server 2008 R2, running Powershell v2, i get strange encoding in my recieved mails even if i try UTF, OEM, ASCII or whatever.

    But when i try the same script in "normal" Powershell shell on our Exchange-server everything works out fine.

    Any idea why?

    Friday, March 22, 2013 9:14 AM
  • This worked for me.Thanks
    Friday, March 17, 2017 12:13 PM
  • Thanks , it helped me :)
    Friday, November 16, 2018 8:02 AM