Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Send Email with PowerShell    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Unicode Issue
Abraham Breuer 
     
12 months ago
I'm facing an issue where, after sending an email using the code below, non-Latin characters (like accents or other special characters) are being converted into question marks (?) in the email body. This happens when the email is actually sent, but the characters appear fine before sending. Can anyone help identify why this is happening and how to fix it?

Here's my code:
Abraham Breuer OP  @Reply  
     
12 months ago
DetailsDim ShellObj As Object, ReturnCode As Integer, CommandStr As String, NewBody As String
    Dim Options As String
    Dim HtmlTempFile As String
    Dim fNum As Integer


    ' Escape double-quotes in body text
    NewBody = Replace(MsgBody2, """", "\""")
    NewBody = Replace(NewBody, vbCrLf, "") ' Remove newlines if any
    
    ' Write HTML body to a temporary file
    HtmlTempFile = Environ$("TEMP") & "\email_body_" & Format(Now, "yyyymmddhhmmss") & ".html"
    fNum = FreeFile
    Open HtmlTempFile For Output As #fNum
    Print #fNum, NewBody
    Close #fNum

    Options = ""
    If Not IsNull(MsgAttach1) Then Options = Options & "-Attachments '" & MsgAttach1 & "' "

    ' Construct PowerShell command using content from the temp file
    CommandStr = "powershell -ExecutionPolicy Bypass -Command " & _
                """$body = Get-Content -Path '" & HtmlTempFile & "' -Raw -Encoding UTF8; " & _
                "$ErrorActionPreference = 'Stop'; " & _
                "try { " & _
                "Send-MailMessage -To '" & "[email protected]" & "' -From '" & MsgFrom & "' " & _
                "-Subject '" & MsgSubject & "' -Body $body -BodyAsHtml " & _
                Options & _
                "-SmtpServer '" & SMTPSERVER & "' -Port 587 -UseSsl -Encoding UTF8 " & _
                "-Credential (New-Object System.Management.Automation.PSCredential('" & USERNAME & "', " & _
                "(ConvertTo-SecureString '" & PASSWORD & "' -AsPlainText -Force))) -Verbose; " & _
                "if ($?) { exit 0 } else { exit 1 } " & _
                "} catch { exit 1 }"""

    ' Create WScript.Shell object
    Set ShellObj = CreateObject("WScript.Shell")
    
    ReturnCode = ShellObj.Run("powershell -Command ""$OutputEncoding = [System.Text.Encoding]::UTF8;""", 0, True)
    ' Execute the command and capture return code
    ReturnCode = ShellObj.Run(CommandStr, 0, True) ' 0=hide window; True=wait until finished
Abraham Breuer OP  @Reply  
     
12 months ago
Solved!!!

you need to use ADODB.Stream instead of the classic open x for output


Abraham Breuer OP  @Reply  
     
12 months ago
Set Stream = CreateObject("ADODB.Stream")
    With Stream
        .Charset = "utf-8"
        .Open
        .WriteText NewBody
        .SaveToFile HtmlTempFile, 2 ' Overwrite
        .Close
    End With
    Set Stream = Nothing

'
'    fNum = FreeFile
'    Open HtmlTempFile For Output As #fNum
'    Print #fNum, NewBody
'    Close #fNum

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Send Email with PowerShell.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 4/30/2026 10:29:10 AM. PLT: 1s