This article was previously published under Q197920
This article has been archived. It is offered "as is" and will no longer be updated.
This is a simple VBScript example of how to automate sending mail throughMicrosoft Outlook.
This example will work in either an Active Server Pages (ASP) page or inWindows Script Host (WSH), as long as there is a valid Outlook UserProfile available on the system. This User Profile appears in the fromfield of the mails generated. It would be possible to set up a Profile thatis used solely for sending automated messages.
Use the following steps with WSH:
Save the code listed below into a file with a .vbs extension. To test,double-click on the file in Windows Explorer.
If you are using Windows 95, you need to install Wsh.exe from the followingWeb site to enable Windows Script Host:
Assuming you are saving it on a Web server with Active Server PageExtensions installed, you can just include the code directly into an ASPpage, or save it in a file that you include into any ASP page that needsmailing capabilities.
Use the following for both methods:
Don't forget to change the recipient and the logon information. The logonhas to be a valid Outlook user profile.
'Body of email message Dim msgBody msgBody="A mail from the Windows Script Host!" 'Call our function with recipient, message and subject MySendMail "email@example.com",msgBody,"Automated Message." Sub MySendMail(recipient,msg,subject) Dim objSession, oInbox, colMessages, oMessage, colRecipients Set objSession = CreateObject("MAPI.Session") objSession.Logon "A Valid User Profile" Set oInbox = objSession.Inbox Set colMessages = oInbox.Messages Set oMessage = colMessages.Add() Set colRecipients = oMessage.Recipients colRecipients.Add recipient colRecipients.Resolve oMessage.Subject = subject oMessage.Text = msg oMessage.Send objSession.Logoff Set objSession = nothing End Sub
NOTE: If you intend to run these scripts from the context of a service, look at article Q177851 in the REFERENCES section below.