Occasionally you will find a need in your VBScript to send someone an email. Maybe you want to notify a user that their software is installed, or send a warning to your teammates when a server goes down, or yourself when a process is finished.
Whatever the reason, the code below can help you. All you need to know is the name or IP address of an SMTP server on your network that you can send email through, the from and to addresses for your email message, the subject line you want to use, and the body of the message. With that information, and the code below, you can send your email.
For example, the following call within your script:
sendEmail "email@example.com",_ "firstname.lastname@example.org",_ "Hey! The Server is Down!",_ "Here is what you need to do to get the server back up...",_ "smtp-server.mycompany.com"
Will send an email from "email@example.com" to "firstname.lastname@example.org" that has the subject line "Hey! The Server is Down!" and a message starting with "Here is what you need". This will be done via the SMTP server found at "smtp-server.mycompany.com".
Sub sendEmail(fromAddress, toAddress, subjectLine, messageBody, smtpServer) 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 = subjectLine objMessage.From = fromAddress objMessage.To = toAddress objMessage.TextBody = messageBody objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort 'Name or IP of Remote SMTP Server objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpServer 'Type of authentication, NONE, Basic (Base64 encoded), NTLM objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoAnonymous 'Server port (typically 25) objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 '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") = 30 objMessage.Configuration.Fields.Update '==End remote SMTP server configuration section== objMessage.Send End Sub