使用CDO.Message對象就可以實現(xiàn),示例代碼演示的是gmail發(fā)送純文本并帶附件的郵件,至于其他的電子郵箱,需要修改代碼中對應的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" '發(fā)件人郵箱
Const Password = "password" '發(fā)件人郵箱密碼
Const Email_To = "380401911@qq.com" '收件人郵箱
Set CDO = CreateObject("CDO.Message") '創(chuàng)建CDO.Message對象
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ī)定必須是這個,我也不知道為什么
With CDO.Configuration.Fields '用with關鍵字減少代碼輸入
.Item(schema & "sendusing") = 2 '使用網(wǎng)絡上的SMTP服務器而不是本地的SMTP服務器
.Item(schema & "smtpserver") = "smtp.gmail.com" 'SMTP服務器地址
.Item(schema & "smtpauthenticate") = 1 '服務器認證方式
.Item(schema & "sendusername") = Email_From '發(fā)件人郵箱
.Item(schema & "sendpassword") = Password '發(fā)件人郵箱密碼
.Item(schema & "smtpserverport") = 465 'SMTP服務器端口
.Item(schema & "smtpusessl") = True '是否使用SSL
.Item(schema & "smtpconnectiontimeout") = 60 '連接服務器的超時時間
.Update '更新設置
End With
CDO.Send '發(fā)送郵件