Outlook: create an e-mail

Using the Outlook application object enables you to access Outlook and create new notes, appointments, e-mails and more. There are a few JScript-scripts at Daren's scripting site (http://www.winscripter.com) dealing with this topic. During writing my WSH Tutorial and my VBA 6.0 Programmers Guide (MS Press Germany), I investigated a bit the Outlook object model. This page discusses a bit the details how to create an e-mail (within Microsoft Outlook 2000) using VBScript (but for JScript it is the same, and the sample should work also for Micrsoft Outlook 97). To access the Outlook objects, you need a reference to the application object. Use the following VBScript command to create an object instance:

Set objOutl = WScript.CreateObject("Outlook.Application")

This command retrieves an object reference to the Outlook Application object. Because we omit here a version number, any installed Outlook version (97/2000) will be invoked. Afterwards you can use the object variable objOutl to access the Application object and its sub-objects from the script.

To create a new e-mail, you need a new mail item. A mail item is an object which may be created using the CreateItem method of the Outlook application object. The following command retrieves the new object:

Set objMailItem = objOutl.CreateItem(olMailItem)

and assigns a reference to the objMailItem object variable. Here I like to mention that the parameter submitted to the CreateItem method is important. I have used olMailItem, which need to be defined within your script (because WSH 1.0 can't access the Type Library with constant definitions, in VB/VBA for instance, these constants are defined already). Here are the definitions for the necessary constants:

Const olMailItem = 0 ' Constants for new items
Const olAppointmentItem = 1
Const olContactItem = 2
Const olTaskItem = 3
Const olJournalItem = 4
Const olNoteItem = 5
Const olPostItem = 6

If you use for instance olNoteItem, you may create a new note using the CreateItem method. But this is beyound the scope of this page (a broad discussion my be found in chapter 11 of my WSH Tutorial). After we are having the item object, it is time to assign the e-mail data. This can be done with assignments to properties, or using methods to add some data. For instance, because an e-mail may have several recipients, we need to apply the Add method to the Recipients collection of the new item. The next two lines shows how to define recipients:

objMailItem.Recipients.Add "SunnyB@xyz.com        ' 1st E-mail address
objMailItem.Recipients.Add "BGate@Microsoft.com"  ' 2nd recipient

The subject and the body of an e-mail may be assignet to the properties using the following statements:

objMailItem.Subject = "My birthday party" ' subject 
objMailItem.Body = "Hi, like to invite you all to my ..." ' the message 

Using these properties and methods it is possible to create all data related to the new mail. After defining the necessary stuff, it is time to "send" this e-mail. To do this, we need a few extra steps. To send e-mails, we need access to the MAPI name space. The required object may be retrieved using the following command:

Set objNameSpace = objOutl.GetNameSpace("MAPI")

The object variable objNameSpace allows you to access the properties and methods of the MAPI name space (object). Here is the sequence to logon (using the default password), execute the send method and logoff.

' now we are ready to "logon" to outlook (using Logon method) 
objNameSpace.Logon "profile", "password"
objMailItem.Send                                    ' send method
objNameSpace.Logoff                                 ' logoff

Depending on your Outlook settings the new e-mail will be send to the SMTP server, or it will be stored into the local Outbox folder (and may be send during the next online session). That's all. Further details may be obtained from the following code listing.

'************************************************
' File:    Outlook.vbs (WSH sample in VBScript) 
' Author:  Günter Born
'
' This script creates a new e-mail entry in Outlook.
'
' A sample derived from my book:
' Windows Scripting Host Tutorial, an E-book available from my WSH Bazaar:
'
' Check out Born's Windows Scripting Host Bazaar at:
' http://www.borncitye.de
'
' In no way shall the author be liable for any
' losses or damages resulting from the use of this
' program. Use AS-IS at your own risk.
'
' The code is the property of the author. You may
' use the code and modify it, as far as this header
' remains intact. Further updates and other samples
' may be found on my site mentioned above.
'************************************************
Option Explicit

Const olMailItem = 0           ' Constants for new items
Const olAppointmentItem = 1
Const olContactItem = 2
Const olTaskItem = 3
Const olJournalItem = 4
Const olNoteItem = 5
Const olPostItem = 6

Dim objOutl, objNameSpace, objMailItem
Dim recipient, message, subject

' define the items for the e-mail
recipient = "user_name@domain.com"
subject = "Subject: Outlook example"
message = "The text of your message"

' Create Outlook object reference
Set objOutl = WScript.CreateObject("Outlook.Application")

' obtain an object reference to "Mapi" name space
Set objNameSpace = objOutl.GetNameSpace("MAPI")

' create a new mail item object
Set objMailItem = objOutl.CreateItem(olMailItem)

' set the mail object properties          
objMailItem.Recipients.Add recipient                ' E-mail address
objMailItem.Recipients.Add "BGates@Microsoft.com"   ' 2nd recipient

objMailItem.Subject = subject                       ' subject 
objMailItem.Body = message                          ' the body text

' now we are ready to "logon" to outlook (using Logon method) 
objNameSpace.Logon "profile", "password"
objMailItem.Send                                    ' send method
objNameSpace.Logoff                                 ' logoff

WScript.Echo "E-mail created for " & recipient      ' Post ready message

' End

Additional information about manipulating Notes, Appointments etc. from WSH scripts may be found in my WSH books.

Back

(c) G. Born