使用CDO.Message對(duì)象就可以實(shí)現(xiàn),示例代碼演示的是gmail發(fā)送純文本并帶附件的郵件,至于其他的電子郵箱,需要修改代碼中對(duì)應(yīng)的smtp服務(wù)器和端口,原理也是一樣的,就不舉例了。貼出來(lái)的代碼沒(méi)有寫(xiě)注釋?zhuān)枰醋⑨尩恼?qǐng)下載源碼。
代碼如下:
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
詳細(xì)注釋版:
代碼如下:
'Date: 2010/6/18
'Author: Demon
'QQ: 380401911
'E-mail: still.demon@gmail.com
'Website: http://demon.tw
Const Email_From = "ddd@163.com" '發(fā)件人郵箱
Const Password = "password" '發(fā)件人郵箱密碼
Const Email_To = "380401911@qq.com" '收件人郵箱
Set CDO = CreateObject("CDO.Message") '創(chuàng)建CDO.Message對(duì)象
CDO.Subject = "From Demon" '郵件主題
CDO.From = Email_From '發(fā)件人地址
CDO.To = Email_To '收件人地址
CDO.TextBody = "Hello world!" '郵件正文
cdo.AddAttachment = "C:\hello.txt" '郵件附件文件路徑
Const schema = "http://schemas.microsoft.com/cdo/configuration/" '規(guī)定必須是這個(gè),我也不知道為什么
With CDO.Configuration.Fields '用with關(guān)鍵字減少代碼輸入
.Item(schema & "sendusing") = 2 '使用網(wǎng)絡(luò)上的SMTP服務(wù)器而不是本地的SMTP服務(wù)器
.Item(schema & "smtpserver") = "smtp.gmail.com" 'SMTP服務(wù)器地址
.Item(schema & "smtpauthenticate") = 1 '服務(wù)器認(rèn)證方式
.Item(schema & "sendusername") = Email_From '發(fā)件人郵箱
.Item(schema & "sendpassword") = Password '發(fā)件人郵箱密碼
.Item(schema & "smtpserverport") = 465 'SMTP服務(wù)器端口
.Item(schema & "smtpusessl") = True '是否使用SSL
.Item(schema & "smtpconnectiontimeout") = 60 '連接服務(wù)器的超時(shí)時(shí)間
.Update '更新設(shè)置
End With
CDO.Send '發(fā)送郵件