Summary: 'Outlook Message Item Wrapper' provides
generic wrapper class which can be used to work with different outlook item types.
Detail: In Outlook Object Model there are different classes for different message type like MailClass, TaskClass, PostClass, ContactClass. However there is no base class (specific
to outlook item) for message.
Because of this we have to use System.Object class as base class. The problem with this approach is whenever we get a reference of any outlook item; we actually get it as of type 'object'.
We have to put a ‘if else’ ladder for type checking & get the actual outlook item object. For e.g. consider following code snippet of 'SelectionChange' event handler, Here I want to print the EntryID of the selected
outlook item,
///
<summary>
/// Event handler for outlook item selection changed event
/// This function prints the Entry ID of the selected Message
/// </summary>
void currentExplorer_SelectionChange()
{
if (this.Application.ActiveExplorer().Selection.Count > 0)
{
Object selectedObject =
this.Application.ActiveExplorer().Selection[1];
string EntryID = string.Empty;
if (selectedObject is Outlook.MailItem)
{
EntryID = ((Outlook.MailItem)selectedObject).EntryID;
}
else if (selectedObject
is Outlook.TaskItem)
{
EntryID = ((Outlook.TaskItem)selectedObject).EntryID;
}
else if (selectedObject
is Outlook.PostItem)
{
EntryID = ((Outlook.PostItem)selectedObject).EntryID;
}
else if (selectedObject
is Outlook.DocumentItem)
{
EntryID = ((Outlook.DocumentItem)selectedObject).EntryID;
}
else if (selectedObject
is Outlook.ContactItem)
{
EntryID = ((Outlook.ContactItem)selectedObject).EntryID;
}
else if (selectedObject
is Outlook.AppointmentItem)
{
EntryID = ((Outlook.AppointmentItem)selectedObject).EntryID;
}
MessageBox.Show(EntryID);
if (selectedObject !=
null)
{
// Release the memory
Marshal.ReleaseComObject(selectedObject);
selectedObject =
null;
}
}
}
Same code using ‘Outlook Message Item Wrapper’
// Reference of Outlook Item Wrapper
OutlookMessageItemWrapper outlookItem;
/// <summary>
/// Event handler for outlook item selection changed event
/// This function prints the Entry ID of the selected Message
/// </summary>
void currentExplorer_SelectionChange()
{
if (this.Application.ActiveExplorer().Selection.Count > 0)
{
Object selectedObject =
this.Application.ActiveExplorer().Selection[1];
string EntryID = string.Empty;
using (outlookItem =
OutlookMessageItemFactory.GetMessageItem(selectedObject))
{
if (outlookItem != null)
{
EntryID = outlookItem.EntryID;
}
}
MessageBox.Show(EntryID);
}
}
Following are the advantages of using Outlook Message Item Wrapper
- Code is much cleaner, no need to put ‘if-else’ ladder in every function
2.
Cleans outlook item memory internally, no need to explicitly call the ‘Marshal.ReleaseComObject’.