如何使用 VBScript 发送带有 Unicode 字符的电子邮件? [英] How to send email with Unicode characters using VBScript?

查看:22
本文介绍了如何使用 VBScript 发送带有 Unicode 字符的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 VBScript 可以发送电子邮件.但是当我尝试发送 Unicode 文本时,结果是不可读的.我尝试了类似 .Charset="UTf-8" 的东西,但没有希望.我的 VBScript 代码如下;emailbody.txt 文件包含类似hệ điều hành khởi động !"

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 = 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

推荐答案

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

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

并且,设置

iMsg.BodyPart.Charset = "utf-8"

这篇关于如何使用 VBScript 发送带有 Unicode 字符的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆