2014-02-23 43 views
2

我想通過VBS發送電子郵件,但我不斷收到錯誤。我希望它發送儘可能簡單的電子郵件。通過VBS腳本發送電子郵件

不行的,與此錯誤:

 Set objEmail = CreateObject("CDO.Message") 
     objEmail.From = "[email protected]" 
     objEmail.To = "[email protected]" 
     objEmail.Subject = "thisisasubject" 
     objEmail.Textbody = "Here is the message" 
     objEmail.Send 

錯誤:

line: 6

Char: 1 error: The "sendusing"configuration value is invalid. 80040220

+0

你沒有告訴你的腳本如何發送消息。請參見[here](http://stackoverflow.com/a/17425519/1630171) –

回答

4
Set emailObj  = CreateObject("CDO.Message") 

emailObj.From  = "[email protected]" 
emailObj.To  = "[email protected]" 

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" 
+0

PS:您的代碼可以在較早的計算機上運行,​​並且它將Outlook的Express默認帳戶用於當前用戶的默認身份。如有必要,它也會嘗試查找其他帳戶。這使預測變得困難,因此請始終指定郵件設置。 –

3
Set MyEmail=CreateObject("CDO.Message") 

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

MyEmail.Subject = "Subject" 
MyEmail.From = "<[email protected]>" 
MyEmail.To  = "<[email protected]>" 
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