2011-08-29 61 views
1

我有一些VBScript發送電子郵件。但是當我嘗試發送Unicode文本時,結果是不可讀的。我嘗試了一些像.Charset="UTf-8"但它是絕望的。我的VBScript代碼如下所示; emailbody.txt文件包含這樣的內容「hưđiềuhànhkhởiđộng!」如何使用VBScript發送帶有Unicode字符的電子郵件?

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

['OpenTextFile'](http://msdn.microsoft.com/en-us/library/314cz14s( v = vs.85).aspx)的第4個參數允許你閱讀Unicode文件 – 2011-08-29 22:42:10

+0

感謝文檔 – Ryo

回答

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" 
+0

謝謝,工作就像一個魅力 – Ryo

相關問題