How To: Send email with embedded text file as body with VBScript

This is an easy VBScript that will allow you to send notifications by mail. Thou applications like SQL have the Database mail, if you have log files which you need to send a text log file from an application as a notification.

Just change the basic email information to your likings and the Mail Server configuration. If it’s an open SMTP server leave the MailSendUsername and MailSendPassword empty. To embed a text file in the body, change the Logfile.txt entry to your filename. Save the below code as anything.vbs and run it as a scheduled task.

Dim objEmail
Set objEmail = CreateObject("CDO.Message")

'************************************
'** Seting basic email information **
'************************************
Const EmailFrom = """My server notification"" "
Const EmailTo = "myboss@myserver.com,myself@myserver.com"
Const EmailSubject = "My Notification"

'***************************************
'** Setting Mail Server Configuration **
'***************************************
Const MailSendUsing = "2"
Const MailSendServer = "smtp.myserver.com"
Const MailSendPort = "25"
Const MailSendUsername = "noreply@myserver.com"
Const MailSendPassword = "mypassword"
Const MailSendAuthenticationType = "1"

'**************************************
'** Email Parameters (DO NOT CHANGE) **
'**************************************
objEmail.From = EmailFrom
objEmail.To = EmailTo
objEmail.Subject = EmailSubject
objEmail.Textbody = EmailBody
objEmail.AddAttachment EmailAttachments
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = MailSendUsing
ObjEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = MailSendServer
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = MailSendPort
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = MailSendAuthenticationType
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = MailSendUsername
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = MailSendPassword

'*******************************************************
'** Setting a text file to be shown in the email Body **
'*******************************************************
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
'** File to be inserted in Body
Const FileToBeUsed = "Logfile.txt"
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
'** Open the file for reading
Set f = fso.OpenTextFile(FileToBeUsed, ForReading)
'** The ReadAll method reads the entire file into the variable BodyText
objEmail.Textbody = f.ReadAll
'** Close the file
f.Close
'** Clear variables
Set f = Nothing
Set fso = Nothing

'* cdoSendUsingPickup (1)
'* cdoSendUsingPort (2)
'* cdoSendUsingExchange (3)

'********************************
'** Parameters (DO NOT CHANGE) **
'********************************
ObjEmail.Configuration.Fields.Update
objEmail.Send

(18315)

Leave a Reply

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