使用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 '发送邮件