VBS通过SMTP发送邮件

  | 转载时请务必以超链接形式标明文章原文链接和作者信息及本版权声明。
原文链接:http://www.liaojl.com/archives/2008/05/vbs-smtp.html

VBS可以通过SMTP发送邮件,先定义发件人,收件人,标题,内容等信息,并指定SMTP服务器地址,调用自定义的SendMail函数就可以发送邮件了。

定义参数及函数调用代码如下:

mfrom="liaojl@liaojl.com"
rcpt="liaojl@liaojl.com, root@liaojl.com"
attachfile=null
serverip="smtp.liaojl.com"
sbj="Subject"
tbody="Hello World"

Call SendMail(mfrom,rcpt,sbj,tbody,attachfile,serverIP)

函数代码如下:

Sub SendMail (mfrom,rcpt,sbj,tbody,attachfile,serverIP)
'######### call SendMail (mfrom,rcpt,cc,sbj,tbody,serverIP) ##################
'Use external smtp server to send mail
if trim(rcpt) = "" or trim(mfrom) = "" then
        exit sub
end if

On Error Resume Next
  Set objEmail = CreateObject("CDO.Message")
  objEmail.From = mfrom
  objEmail.To = rcpt
'  objEmail.Cc = cc
'  objEmail.Bcc = bcc
  objEmail.Subject = sbj
  objEmail.Textbody = tbody
'Sending an HTML e-mail:
'  objEmail.HTMLBody = "

This is a message.

" 'Sending an HTML e-mail that sends a webpage from a website: ' objEmail.CreateMHTMLBody "file://c:/mydocuments/test.htm" If Trim(attachfile) <> "" Then objEmail.AddAttachment attachfile objEmail.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 objEmail.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = serverIP objEmail.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 '-------Type of authentication, NONE, Basic (Base64 encoded), NTLM-------- 'objEmail.Configuration.Fields.Item _ '("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic '-------Your UserID/Passwd on the SMTP server for SMTP auth for mail sent----- 'objEmail.Configuration.Fields.Item _ '("http://schemas.microsoft.com/cdo/configuration/sendusername") = "USER_NAME" 'objEmail.Configuration.Fields.Item _ '("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "PASSWORD" '-------Use SSL for the connection (False or True)--------- 'objEmail.Configuration.Fields.Item _ '("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False 'Connection Timeout in seconds (the maximum time CDO will try to establish a '--------connection to the SMTP server)--------- 'objEmail.Configuration.Fields.Item _ '("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60 objEmail.Configuration.Fields.Update objEmail.Send set objEmail=nothing End Sub

Leave a comment

Archives

Creative Commons License
This blog is licensed under a Creative Commons License.