Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBScript to send email without running Outlook

I have written an automated test that runs each night, and I would like to email the results each night once the test is finished.

In order to do this I attempted to put the following at the end of my batchfile:

Set MyApp = CreateObject("Outlook.Application")
Set MyItem = MyApp.CreateItem(0)
With MyItem
    .To = "[email protected]"
    .Subject = "Subject"
    .ReadReceiptRequested = False
    .HTMLBody = "resport"
End With
MyItem.Send

However, this is causing the email to not send because my Outlook is not open, as the test is run in the background, and I have no access to the UI.

Is there anyway to send this email without actually running outlook on the machine.

Thanks!

like image 527
user856354 Avatar asked Aug 12 '11 14:08

user856354


People also ask

How do I send an automated email in VBScript?

Just insert your desired parameters such as smtp mail server address, credentials, email addresses and content of the email with attachment within the provided variables inside the script and execute the script to start sending email instantaneously.

How do I run a VBS file?

VBS file extension. Double-click the script. This runs the Visual Basic script. If the script is a console script, it will open a command prompt window and display the output.

What is VB scripting language?

VBScript ("Microsoft Visual Basic Scripting Edition") is an Active Scripting language developed by Microsoft that is modeled on Visual Basic.


1 Answers

You can send email without Outlook in VBScript using the CDO.Message object. You will need to know the address of your SMTP server to use this:

Set MyEmail=CreateObject("CDO.Message")

MyEmail.Subject="Subject"
MyEmail.From="[email protected]"
MyEmail.To="[email protected]"
MyEmail.TextBody="Testing one two three."

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.Update
MyEmail.Send

set MyEmail=nothing

If your SMTP server requires a username and password then paste these lines in above the MyEmail.Configuration.Fields.Update line:

'SMTP Auth (For Windows Auth set this to 2)
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")=1
'Username
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername")="username" 
'Password
MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword")="password"

More information on using CDO to send email with VBScript can be found on the link below: http://www.paulsadowski.com/wsh/cdo.htm

like image 69
michaelx386 Avatar answered Oct 01 '22 11:10

michaelx386