How to send email from a VBS file.

Local Email Server

This is the standard method. It assumes an SMTP email server is installed on your machine. This is the case on most web servers.

 Set objMessage = CreateObject("CDO.Message") 
 objMessage.Subject = "Subject"
 objMessage.Sender = "[email protected]"
 objMessage.To = "[email protected]"
 objMessage.TextBody = "iMacros script completed. Status = OK"
 objMessage.Send

Substitute your own email information and message text. You can insert this code into any script that requires a message to be sent.

Remote Email Server

This code shows you how to use a remotes server rather than the SMTP server on your own machine.

 Set objMessage = CreateObject("CDO.Message") 
 objMessage.Subject = "iMacros Message" 
 objMessage.From = "[email protected]" 
 objMessage.To = "[email protected]" 
 objMessage.TextBody = "iMacros script completed. Status = OK

 '==This section provides the configuration information for the remote SMTP server.
 '==Normally you will only change the server name or IP.
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
 'Name or IP of Remote SMTP Server
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.myserver.com"
 'Server port (typically 25)
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25  
 objMessage.Configuration.Fields.Update
 '==End remote SMTP server configuration section==

 objMessage.Send

Remote Email Server with Password

Sending a text email using authentication against a remote SMTP server. More and more administrators are restricting access to their servers to control spam or limit which users may utilize the server. This example shows you how to use basic authentication, the most commonly used authentication method, when the SMTP server you are using requires it.

This code is slightly more complex but not very difficult to understand or work with.

 Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. 
 Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network). 

 Const cdoAnonymous = 0 'Do not authenticate
 Const cdoBasic = 1 'basic (clear-text) authentication
 Const cdoNTLM = 2 'NTLM

 Set objMessage = CreateObject("CDO.Message") 
 objMessage.Subject = "iMacros Status Message" 
 objMessage.From = """iMacros"" " 
 objMessage.To = "[email protected]" 
 objMessage.TextBody = "iMacros script completed. Status = OK"

 '==This section provides the configuration information for the remote SMTP server.

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

 'Name or IP of Remote SMTP Server
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.your.com"

 'Type of authentication, NONE, Basic (Base64 encoded), NTLM
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic

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

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

 'Server port (typically 25)
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

 'Use SSL for the connection (False or True)
 objMessage.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)
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

 objMessage.Configuration.Fields.Update

 '==End remote SMTP server configuration section==  

 objMessage.Send

Send email with MAPI

This solution assumes that Outlook or another email client with MAPI support is installed.

 Dim ToAddress
 Dim MessageSubject
 Dim MessageBody
 Dim MessageAttachment

 Dim ol, ns, newMail

 ToAddress = "Jim, Test"   ' change this...
 MessageSubject = "iMacros"
 MessageBody = "iMacros Status = OK"

 Set ol = WScript.CreateObject("Outlook.Application")
 Set ns = ol.getNamespace("MAPI")
 ns.logon "","",true,false
 Set newMail = ol.CreateItem(olMailItem)
 newMail.Subject = MessageSubject
 newMail.Body = MessageBody & vbCrLf

 ' validate the recipient, just in case...
 Set myRecipient = ns.CreateRecipient(ToAddress)
 myRecipient.Resolve
 If Not myRecipient.Resolved Then
 MsgBox "unknown recipient"
 Else
    newMail.Recipients.Add(myRecipient)
    newMail.Send
 End If

 Set ol = Nothing

Source: http://wiki.imacros.net/send-email.vbs

Leave a Reply

Your email address will not be published. Required fields are marked *