15

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 = "a@a.com"
    .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!

user856354
  • 263
  • 3
  • 5
  • 21
  • http://stackoverflow.com/questions/6588621/how-to-send-email-to-a-distribution-list-with-vbscript-in-an-asp –  Aug 12 '11 at 15:53

2 Answers2

29

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="name@domain.com"
MyEmail.To="a@a.com"
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

michaelx386
  • 745
  • 8
  • 8
  • Thank you very much! This helps a ton! I requested the name of the server and should hopefully have it by Monday. Then I'll select this as the accepted answer. – user856354 Aug 12 '11 at 19:55
  • What if I want username and machine name to be included in mail like `userxyz logged in to machinexyz`. Actually I have Windows 7 client on which I want to track logins by sending mails. So I will be calling this code as login event task. I have Windows Server 2008 which manages AD. So where should I configure [this](http://superuser.com/questions/660703/getting-mail-on-windows-logon-with-username-of-logged-in-user) rule - on win 7 client or win server. Will that make any difference? Also is it possible to achieve this using `vbs`? – Mahesha999 Oct 16 '13 at 15:29
0

Yes. Blat or any other self contained SMTP mailer. Blat is a fairly full featured SMTP client that runs from command line

Blat is here

MJB
  • 9,088
  • 6
  • 30
  • 48