通过VBS脚本发送电子邮件

时间:2014-02-23 19:48:04

标签: email vbscript smtp

我正在尝试通过VBS发送电子邮件但我一直收到错误。我希望它尽可能简单地发送电子邮件。

不起作用,出现此错误:

        Set objEmail = CreateObject("CDO.Message")
        objEmail.From = "othermail@hey.com"
        objEmail.To = "myemail@yahoo.com"
        objEmail.Subject = "thisisasubject" 
        objEmail.Textbody = "Here is the message"
        objEmail.Send

错误:

  

行:6

     

字符:1错误:“sendusing”配置值无效。 80040220

2 个答案:

答案 0 :(得分:5)

Set emailObj      = CreateObject("CDO.Message")

emailObj.From     = "dc@gail.com"
emailObj.To       = "dc@gail.com"

emailObj.Subject  = "Test CDO"
emailObj.TextBody = "Test CDO"

Set emailConfig = emailObj.Configuration

emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing")    = 2  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1  
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl")      = true 
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername")    = "YourUserName"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword")    = "Password1"

emailConfig.Fields.Update

emailObj.Send

If err.number = 0 then Msgbox "Done"

答案 1 :(得分:3)

Set MyEmail=CreateObject("CDO.Message")

Const cdoBasic=0 'Do not Authenticate
Const cdoAuth=1 'Basic Authentication

MyEmail.Subject = "Subject"
MyEmail.From    = "<fromsample@mail.com>"
MyEmail.To      = "<tosample@mail.com>"
MyEmail.TextBody= "TEST MAIL"

MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2

'SMTP Server
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.server.com"

'SMTP Port
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25


MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1

'Your UserID on the SMTP server
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"

'Your password on the SMTP server
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"


'Use SSL for the connection (False or True)
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False

MyEmail.Configuration.Fields.Update
MyEmail.Send

Set MyEmail=nothing