如何使用VBScript发送带有Unicode字符的电子邮件?

时间:2011-08-29 16:59:57

标签: html email unicode vbscript

我有一些VBScript发送电子邮件。但是当我尝试发送Unicode文本时,结果是不可读的。我试过像.Charset="UTf-8"这样的东西,但它没有希望。我的VBScript代码如下; emailbody.txt文件包含这样的内容“hệđiềuhànhkhở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 = CreateObject("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

1 个答案:

答案 0 :(得分:2)

使用Adodb.Stream对象阅读电子邮件正文。因为FSO不支持读取utf-8编码文件 像下面这样填充BodyText变量:

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

并设置

iMsg.BodyPart.Charset = "utf-8"