Send-MailMessage converts apostrophe to question mark

This topic contains 4 replies, has 3 voices, and was last updated by  Mahin 1 week, 3 days ago.

  • Author
    Posts
  • #98097

    Mahin
    Participant

    Hi folks. I'm trying to setup sending emails via PowerShell and have the code to do it, but every time i use Send-MailMessage to send the body, it converts apostrophe symbol to question mark. Can someone advise what i'm doing wrong? I tried to convert it to UTF8 and ASCII but that made it worse. Thank you

    $message = get-content c:\mail\sample.txt | out-string

    Send-mailmessage -To fakeuser@contso.com -From testuser@contso.com -smtpserver "196.192.1.1" -subject "Test email" -Body $message

    The sample.txt file in c:\temp has the correct message format like this below

    "Please don't send out wrong requests for support"

    But once i execute the PS script, and get the email, it is written incorrectly as below

    "Please don?t send out wrong requests for support"

  • #98169

    Fredrik Kacsmarck
    Participant

    A couple of things to try.

    1. Check how the string looks in the variable, is it correct if you do:

    Write-Output $message
    

    2. Send-Mailmessage also has an Encoding parameter, so try:

    Send-mailmessage -To fakeuser@contso.com -From testuser@contso.com -smtpserver "196.192.1.1" -subject "Test email" -Body $message -Encoding utf8
    
    • #98304

      Mahin
      Participant

      Hi Fredrik, it looks like your solution of adding -encoding UTF8 at the end did the trick 🙂 Thanks so much!

  • #98187

    Joel Sallow
    Participant

    Hmm. Instead of using Out-String, try simply supplying the -Raw parameter to Get-Content to import the file as a single block of text and see if that clears things up a bit for you. 🙂

    • #98307

      Mahin
      Participant

      Joel, thank you much for your reply as well 🙂

You must be logged in to reply to this topic.