VBScript to send email without running Outlook
Asked Answered
P

4

16

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!

Parris answered 12/8, 2011 at 14:54 Comment(1)
#6589121Vesuvius
S
33

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

Scenarist answered 12/8, 2011 at 15:13 Comment(3)
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.Parris
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 rule - on win 7 client or win server. Will that make any difference? Also is it possible to achieve this using vbs?Prostate
The link for more info is broken.Onega
G
1

This is how you can send emails from other services like private email

Set objMessage = CreateObject("CDO.Message") 
 objMessage.Subject = "Wooow" 
 objMessage.From = "[email protected]" 
 objMessage.To = "[email protected]" 
 objMessage.TextBody = "You are awesome"
 Set objConfig = objMessage.Configuration
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "serverXXXXXX.web-hosting.com"
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "[email protected]"
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "YOUR_PASSWORD_XXXX"
 'Server port (typically 25)
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 4XX
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
 objConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
 objConfig.Fields.Update
 objMessage.Send

I tested it, it's working.

Gothar answered 29/7, 2023 at 18:13 Comment(0)
S
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

Strom answered 12/8, 2011 at 14:58 Comment(0)
O
0

Here is a current link to the schema documentation:

https://learn.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2003/ms873029(v=exchg.65)

Instead of using the object references over and over again, a nice With statement will look better:

Set Msg = CreateObject("CDO.Message")
With Msg
 .To = "[email protected]"
 .From = "[email protected]"
 .Subject = "Subject goes here."
 .TextBody = "Message goes here."
End With
With Msg.Configuration
 .Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
 .Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.example.com"
 .Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
 .Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"
 .Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
 .Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
 .Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = false
 .Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 15
 .Fields.Update
End With
Msg.Send

Extra note about the smtpusessl field: This works on a TLS type port only. It is not the same as the STARTTLS protocol. Simply set it to false when using STARTTLS.

Onega answered 22/5 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.