执行vbs脚本文件的程序为: system32文件下的 。
下面vbs脚本代码可以完成用"chen@gmail.com"账号发送一封邮件给"kekec@qq.com"。
1
NameSpace
=
"
http://schemas.microsoft.com/cdo/configuration/
"
2 Set Email = CreateObject ( " CDO.Message " )
3 Email.From = " chen@gmail.com " ' 发件邮件地址
4 Email.To = " kekec@qq.com " ' 收件邮件地址
5 Email.Subject = " Test_ " + cstr ( now ()) ' 邮件主题
6 Email.Textbody = " OK! It is only a test. " ' 邮件内容
7 Email.AddAttachment " D:\My Documents\My Pictures\809041233006998.jpg " ' 所带附件
8 With Email.Configuration.Fields
9 .Item(NameSpace & " sendusing " ) = 2 ' CDO发送端口号
10 .Item(NameSpace & " smtpserver " ) = " smtp.gmail.com " ' SMTP服务器
11 .Item(NameSpace & " smtpserverport " ) = 465 ' SMTP服务器端口号
12 .Item(NameSpace & " smtpusessl " ) = true ' SMTP服务器是否使用了SSL
13 .Item(NameSpace & " smtpauthenticate " ) = 1 ' 认证方式是BASIC
14 .Item(NameSpace & " sendusername " ) = " chen@gmail.com " ' 发件人邮箱账号
15 .Item(NameSpace & " sendpassword " ) = " ************ " ' 发件人邮箱密码
16 .Update
17 End With
18 Email.Send
2 Set Email = CreateObject ( " CDO.Message " )
3 Email.From = " chen@gmail.com " ' 发件邮件地址
4 Email.To = " kekec@qq.com " ' 收件邮件地址
5 Email.Subject = " Test_ " + cstr ( now ()) ' 邮件主题
6 Email.Textbody = " OK! It is only a test. " ' 邮件内容
7 Email.AddAttachment " D:\My Documents\My Pictures\809041233006998.jpg " ' 所带附件
8 With Email.Configuration.Fields
9 .Item(NameSpace & " sendusing " ) = 2 ' CDO发送端口号
10 .Item(NameSpace & " smtpserver " ) = " smtp.gmail.com " ' SMTP服务器
11 .Item(NameSpace & " smtpserverport " ) = 465 ' SMTP服务器端口号
12 .Item(NameSpace & " smtpusessl " ) = true ' SMTP服务器是否使用了SSL
13 .Item(NameSpace & " smtpauthenticate " ) = 1 ' 认证方式是BASIC
14 .Item(NameSpace & " sendusername " ) = " chen@gmail.com " ' 发件人邮箱账号
15 .Item(NameSpace & " sendpassword " ) = " ************ " ' 发件人邮箱密码
16 .Update
17 End With
18 Email.Send
收到的邮件效果图:
PS: "smtpserver" "smtpserverport" "smtpusessl" 等信息设置不对时,脚本会弹出错误对话框,以示邮件发送失败。
126邮箱作为发信邮箱(smtpserver修改为smtp.126.com,SMTP默认是开启的,如下图所示)
qq邮箱作为发信邮箱(smtpserver修改为smtp.qq.com,SMTP默认是关闭的,另外qq邮箱对于第三方客户端发信,密码使用授权码的方式)
“设置” -- “帐号” -- “POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务”
若SMTP服务不通的话,会提示如下错误: