In recent articles I’ve covered working with various pieces
of the Microsoft Office Suite including Word and Excel. Today,
I’ll continue the concept by manipulating Microsoft Outlook via Microsoft .NET
code.
Working with Outlook
As with other Microsoft Office products, Visual Basic
for Applications (VBA) is utilized. Consequently, a little COM (Component
Object Model) knowledge comes in handy. However, the .NET
COM interop feature simplifies the process for
.NET developers. Thus, you can easily utilize COM objects within a .NET
application.
The Outlook object model exposes many features of the
Outlook environment. This includes the following functionality:
- E-mail: create, send, and
manipulate mail items - Contacts database: Utilize the
Outlook contacts databases. You may add new contacts, edit existing, or
integrate with other applications like creating a letter via Word and
information from an Outlook contact. - Calendar: create, edit, delete,
and manipulate individual calendar entries - Notes and Tasks: Interact with the
notes and tasks portions of Outlook - Outlook user interface: Control or
manipulate explorers, inspectors, command bars, and so forth.
Microsoft supplies an interop
assembly, which ships with Outlook 2003. This assembly, named Microsoft.Office.Interop.Outlook.dll,
resides in the Global Assembly Cache under the name
Microsoft.Office.Interop.Outlook.dll. To reference this assembly from Visual
Studio .NET 2003, access the COM tab from the Add References dialog box and
select Microsoft Outlook 11.0 Object Library.
Outlook object model
Outlook exposes an overwhelming number of objects with
associated methods, properties, and so forth. Thankfully, their functions are
documented and available with an installation of Microsoft Office. The help
files are located on the hard drive (if help is installed) in the following
path:
<drive letter>:\Program Files\Microsoft Office\OFFICE11\1033
There will be help files for all Office applications
installed. A drawback of the help documentation is all code examples are
presented via VBScript, so you will have to be aware of this difference when
trying to apply to the .NET Framework. With that said, let’s take a closer look
at individual entities within the Outlook object model. We’ll focus on the
following items:
- Application: The Outlook
application environment. This is the root object necessary to work with
Outlook. - Namespace: The Namespace object
represents the messaging service provider. The MAPI namespace is required
to gain access to Outlook folders and items. - MailItem: The MailItem
object allows you to work with Outlook mail memos. There are various item
objects available to work with the various item types including notes, calendar
entries, contacts, and so forth. - MAPIFolder: If you use Outlook, you’re
familiar with folders. (I’m often chastised for my organized Outlook
inbox, with everything filed in the appropriate folder.) The MAPIFolder object provides a vehicle for working with
mail folders.
Let’s put these objects to use by creating and sending e-mails via Outlook.
Sending e-mail
In the next example, a simple mail message is created and
sent via Outlook.
Listing A uses C#, while
Listing B shows the example with VB.NET.
A few notes on the code:
- A
copy of the mail message is copied to the Drafts folder for the purpose of
demonstrating how placing a message in a folder works. The olDefaultFolders object contains a list of the default
folders included with Outlook. - The
creation of a mail item uses the CreateItem
method of the Outlook.Application class. The
call to this method requires a cast to the necessary Item type (in this
case, MailItem). - The
various properties of a mail message are included as properties of the MailItem class: to, subject, body, and so forth. - The SaveSentMessageFolder property of the MailItem class allows you to designate where a copy of
the message should be saved (it is optional). - The
Send method of the MailItem class sends the
message via Outlook. - Message
creation is included in a try/catch block that handles COM exceptions. The
COMException class is available in the System.Runtime.InteropServices namespace, so this
namespace should be included in the code.
Outlook security
Security is an important aspect of Outlook, and allowing
external code to create and send mail messages is considered a big security
risk. When and if you execute the code in the previous example, you will see an
Outlook security dialog box that says “A program is trying to
automatically send e-mail on your behalf. Do you want to allow this? If this is
unexpected, it may be a virus and you should choose ‘No’.” The dialog box
contains Yes, No, and Help buttons, so the user may cancel the operation with a
simple click of the mouse.
Creating an appointment
In the next example, we create an Outlook appointment to
appear on the user’s calendar. Once again, the base Application object is
utilized. In addition, an AppointmentItem is used as
opposed to the previously used MailItem since we are
creating a calendar entry. Listing C
uses C# to demonstrate how you may accomplish this. Listing D provides the example in VB.NET.
A few notes on the code:
- The AppointmentItem class includes various properties for
setting up an appointment. In this example, we utilize the Subject (what
appears in the calendar), body (visible when an entry is opened), location, start date/time, and end date/time. - The ReminderSet property of the AppointmentItem
class determines if a reminder is displayed to the user. The associated ReminderMinutesBeforeStart property determines when
the reminder will be displayed. - The
Importance property of the AppointmentItem class
sets the item’s importance. Values for the property are set via the OlImporantance enumerator, which has the following
values: olImportanceHigh, olImportanceLow,
and olImportanceNormal. - The BusyStatus property determines how the appointment is
displayed on the calendar (i.e., whether the time is blocked off as busy,
tentative, free, and so forth. The OlBusyStatus
enumerator contains the following list of possible values: olBusy, olFree, olOutOfOffice, and olTentative.
Another developer option
Microsoft Office is the most popular office productivity
suite in the world, and it is a desktop standard for most large organizations.
For this reason, it is often available for you to take advantage of in your
.NET applications. A Windows Forms application may easily utilize the mail
features of Outlook and, likewise, add calendar appointments or contact
database entries with very little code. The interoperability between the .NET
platform and Microsoft Office greatly expand the developer’s options.
TechRepublic’s free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically sign up today!