欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

VBS中用CDO.Message发送邮件的实现代码

程序员文章站 2024-01-15 23:08:04
使用cdo.message对象就可以实现,示例代码演示的是gmail发送纯文本并带附件的邮件,至于其他的电子邮箱,需要修改代码中对应的smtp服务器和端口,原理也是一样的,...
使用cdo.message对象就可以实现,示例代码演示的是gmail发送纯文本并带附件的邮件,至于其他的电子邮箱,需要修改代码中对应的smtp服务器和端口,原理也是一样的,就不举例了。贴出来的代码没有写注释,需要看注释的请下载源码。
复制代码 代码如下:

const email_from = "still.demon@gmail.com"
const password = "password"
const email_to = "380401911@qq.com"
set cdo = createobject("cdo.message")
cdo.subject = "from demon"
cdo.from = email_from
cdo.to = email_to
cdo.textbody = "hello world!"
cdo.addattachment = "c:\hello.txt"
const schema = "http://schemas.microsoft.com/cdo/configuration/"
with cdo.configuration.fields
.item(schema & "sendusing") = 2
.item(schema & "smtpserver") = "smtp.gmail.com"
.item(schema & "smtpauthenticate") = 1
.item(schema & "sendusername") = email_from
.item(schema & "sendpassword") = password
.item(schema & "smtpserverport") = 465
.item(schema & "smtpusessl") = true
.item(schema & "smtpconnectiontimeout") = 60
.update
end with
cdo.send

详细注释版:
复制代码 代码如下:

'date: 2010/6/18
'author: demon
'qq: 380401911
'e-mail: still.demon@gmail.com
'website: http://demon.tw

const email_from = "ddd@163.com" '发件人邮箱
const password = "password" '发件人邮箱密码
const email_to = "380401911@qq.com" '收件人邮箱

set cdo = createobject("cdo.message") '创建cdo.message对象
cdo.subject = "from demon" '邮件主题
cdo.from = email_from '发件人地址
cdo.to = email_to '收件人地址
cdo.textbody = "hello world!" '邮件正文
cdo.addattachment = "c:\hello.txt" '邮件附件文件路径
const schema = "http://schemas.microsoft.com/cdo/configuration/" '规定必须是这个,我也不知道为什么

with cdo.configuration.fields '用with关键字减少代码输入
.item(schema & "sendusing") = 2 '使用网络上的smtp服务器而不是本地的smtp服务器
.item(schema & "smtpserver") = "smtp.gmail.com" 'smtp服务器地址
.item(schema & "smtpauthenticate") = 1 '服务器认证方式
.item(schema & "sendusername") = email_from '发件人邮箱
.item(schema & "sendpassword") = password '发件人邮箱密码
.item(schema & "smtpserverport") = 465 'smtp服务器端口
.item(schema & "smtpusessl") = true '是否使用ssl
.item(schema & "smtpconnectiontimeout") = 60 '连接服务器的超时时间
.update '更新设置
end with

cdo.send '发送邮件

原文:http://demon.tw/programming/vbs-send-email.html