How to send email with Unicode characters using VBScript?
I have some VBScript to send an email. But when I try to send text which is Unicode, the result is something that is unreadable. I tried something like .Charset="UTf-8"
but it's hopeless. My VBScript code is below; the emailbody.txt file contains something like this "hệ điều hành khởi động !"
Option Explicit
Call Email
Function Email
Dim iMsg, iConf, Flds, schema
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields
schema = "http://schemas.microsoft.com/cdo/configuration/"
Flds.Item(schema & "sendusing") = 2
Flds.Item(schema & "smtpserver") = "smtp.gmail.com"
Flds.Item(schema & "smtpserverport") = 465
Flds.Item(schema & "smtpauthenticate") = 1
Flds.Item(schema & "sendusername") = ""
Flds.Item(schema & "sendpassword") = ""
Flds.Item(schema & "smtpusessl") = 1
Flds.Update
'These constants are defined to make the code more readable
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f, BodyText, HeadText
Set fso = C开发者_运维百科reateObject("Scripting.FileSystemObject")
'Open the file for reading
Set f = fso.OpenTextFile("emailbody.txt", ForReading) 'edit path if required
'The ReadAll method reads the entire file into the variable BodyText
BodyText = f.ReadAll
'Close the file
f.Close
Set f = Nothing
Set fso = Nothing
With iMsg
.To = ""
.From = ""
.Sender = ""
.Subject = ""
.HTMLBody = BodyText
Set .Configuration = iConf
.Send
End With
set iMsg = nothing
set iConf = nothing
set Flds = nothing
End Function
Read the email body using Adodb.Stream object. Because FSO does not support reading utf-8 encoded files.
Fill the BodyText variable like that:
Dim adoStream
Set adoStream = CreateObject("Adodb.Stream")
adoStream.Open
adoStream.Charset = "UTF-8"
adoStream.LoadFromFile "emailbody.txt"
'********** !! ***************
BodyText = adoStream.ReadText(-1)
'********** !! ***************
adoStream.Close
Set adoStream = Nothing
And, set
iMsg.BodyPart.Charset = "utf-8"
精彩评论