Microsoft Knowledge Base Email Alertz

(197920) - This is a simple VBScript example of how to automate sending mail through Microsoft Outlook. This example will work in either an Active Server Pages (ASP) page or in Windows Script Host (WSH), as long as there is a valid Outlook User Profile...

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 197920 - Last Review: August 8, 2007 - Revision: 3.6

HOWTO: Windows Script Host Script for Sending E-Mail

System TipThis article applies to a different version of Windows than the one you are using. Content in this article may not be relevant to you. Visit the Windows Vista Solution Center
This article was previously published under Q197920

SUMMARY

This is a simple VBScript example of how to automate sending mail through Microsoft Outlook.

MORE INFORMATION

This example will work in either an Active Server Pages (ASP) page or in Windows Script Host (WSH), as long as there is a valid Outlook User Profile available on the system. This User Profile appears in the from field of the mails generated. It would be possible to set up a Profile that is 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 following Web site to enable Windows Script Host:
http://msdn2.microsoft.com/en-us/library/ms950396.aspx (http://msdn2.microsoft.com/en-us/library/ms950396.aspx)
Use the following steps with ASP:

Assuming you are saving it on a Web server with Active Server Page Extensions installed, you can just include the code directly into an ASP page, or save it in a file that you include into any ASP page that needs mailing capabilities.

Use the following for both methods:

Don't forget to change the recipient and the logon information. The logon has 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 "someone@example.microsoft.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.

REFERENCES

191430  (http://kbalertz.com/Feedback.aspx?kbNumber=191430/EN-US/ ) Run a WSH file from NT Scheduler
177851  (http://kbalertz.com/Feedback.aspx?kbNumber=177851/EN-US/ ) Build a VB/Messaging Application to Run from a Service
For more information about the technologies discussed here, please refer to the article "Getting Started with ASP Messaging" in the MSDN Online:

http://msdn.microsoft.com/en-us/library/ms972348.aspx (http://msdn.microsoft.com/en-us/library/ms972348.aspx)

APPLIES TO
  • Microsoft Windows Scripting Host 2.5
  • Microsoft Visual Basic, Scripting Edition 3.0
  • Microsoft Windows NT 4.0
  • Microsoft Windows 95
  • Microsoft Windows 98 Standard Edition
Keywords: 
kbautomation kbhowto kbscript KB197920
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate