Click here to Skip to main content
Click here to Skip to main content

Using MAPI properties and events in .NET with the MAPI Store Accessor

, 24 Jan 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple WinForms application that shows MAPI folders and items in your mail profile.

A running application

A PR_BODY value of the selected item

Introduction

This article covers the basics of writing a simple application using Extended MAPI. The main purpose of this article is to show developers how they can enhance their applications with Extended MAPI. To have this application working, you need to download and install a free component, the MAPI Store Accessor for .NET.

The MAPI Store Accessor

The MAPI Store Accessor is a .NET component which allows you to subscribe to such events as OnNewMail, OnObjectChanged, OnObjectCopied, etc. It also lets you access MAPI stores, folders, items, and attachments. You will see that the use of the MAPI Store Accessor effectively hides the complexities of MAPI.

Currently, the MAPI Store Accessor for .NET supports:

  • Visual Studio 2005 ( Standard and Professional )
  • Visual Studio 2008 ( Standard and Professional )

The fundamental classes in the AddinExpress.MAPI library are:

  • Store - represents a store in a profile
  • Folder - represents a folder in a store
  • MapiItem - represents an item in a folder
  • HiddenItem - represents a hidden item in a folder
  • Attachment - represents an attachment

There are also corresponding collections such as Stores, Folders etc. To start using these classes, you should get the collection of stores from the component when it is connected to the MAPI subsystem.

First steps

To start working with the MAPI Store Accessor, find it in the Toolbox window and drop it onto the Windows Form, or declare the component in your code manually.

There are two important things you should do:

  • Call Initialize
  • Call LogOff

They are like start and stop commands. When calling Initialize with the boolean parameter, the component connects to the default profile. Your application creates or connects to the existing MAPI session depending on the value which you pass to the method. If you want to create a new session, pass True. If you need to connect to the existing session, pass False. When you pass False, make sure that the session exists. Another overload of the Initialize method accepts a profile name and password.

Coding

Add Initialize and LogOff where appropriate. I do it in the Load and FormClosing event handlers of the form, respectively.

VB.NET

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles MyBase.Load
    ' initialize component
    AdxmapiStoreAccessor1.Initialize(True)
    ' additional tasks
    Dim folders As Folders = _
        AdxmapiStoreAccessor1.MsgStores(0).RootFolder.Folders(1).Folders
    Dim folder As Folder
    For Each folder In folders
       Dim folderName As Object = folder.GetProperty(ADXMAPIPropertyTag._PR_DISPLAY_NAME)
       If folderName IsNot Nothing Then
                 ipmTreeView.Nodes.Add(folderName.ToString())
       End If
    Next
End Sub

Private Sub Form1_FormClosing(ByVal sender As System.Object, _
       ByVal e As System.Windows.Forms.FormClosingEventArgs) _
       Handles MyBase.FormClosing
    AdxmapiStoreAccessor1.LogOff()
End Sub

C#

private void Form1_Load(object sender, EventArgs e)
{
    // initialize component
    adxmapiStoreAccessor1.Initialize(true);
    
    // additional tasks
    foreach (Folder folder in 
       adxmapiStoreAccessor1.MsgStores[0].RootFolder.Folders[1].Folders)
    {
        object folderName = folder.GetProperty(ADXMAPIPropertyTag._PR_DISPLAY_NAME);
        if (folderName != null)
        {
            ipmTreeView.Nodes.Add(folderName.ToString());
        }
    }
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    adxmapiStoreAccessor1.LogOff();
}

Now, we are ready to add business logic to our application. We need a TreeView and a ListBox. Drop them onto the form. Add the appropriate settings of your choice. Finally, add the following event handlers:

  • The AfterSelect event handler for the TreeView control:
  • VB.NET

    Private Sub ipmTreeView_AfterSelect(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.TreeViewEventArgs) _
        Handles ipmTreeView.AfterSelect
        listBoxItems.Items.Clear()
        ' retrieve the first message store in a profile we are logged in
        current = _
           AdxmapiStoreAccessor1.MsgStores(0).RootFolder.Folders(1).Folders(e.Node.Index)
        Dim item As MapiItem
        For Each item In current.MapiItems
           ' retrieve the PR_BODY property of the mapi item
           Dim subject As Object = item.GetProperty(ADXMAPIPropertyTag._PR_SUBJECT)
           If subject IsNot Nothing Then
                 listBoxItems.Items.Add(subject.ToString())
           End If
        Next
    End Sub

    C#

    private void ipmTreeView_AfterSelect(object sender, TreeViewEventArgs e)
    {
        listBoxItems.Items.Clear();    
        // retrieve the first message store in a profile we are logged in
        current = ((Folders)adxmapiStoreAccessor1.MsgStores[0].
                      RootFolder.Folders[1].Folders).Add(e.Node.Text);    
        foreach (MapiItem item in current.MapiItems)
        {
            // retrieve the PR_BODY property of the mapi item
            object subject = item.GetProperty(ADXMAPIPropertyTag._PR_SUBJECT);
            if (subject != null)
            {
                listBoxItems.Items.Add(subject.ToString());
            }
        }
    }
  • The DoubleClick event handler for the ListBox control:
  • VB.NET

    Private Sub listBoxItems_DoubleClick(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles listBoxItems.DoubleClick
         If listBoxItems.SelectedIndex >= 0 Then
             Dim item As MapiItem = current.MapiItems(listBoxItems.SelectedIndex)
             Dim body As Object = item.GetProperty(ADXMAPIPropertyTag._PR_BODY)
             If body IsNot Nothing Then
                MsgBox(body.ToString(), "The body of the mail item", _
                   MessageBoxButtons.OK, MessageBoxIcon.Information)
             End If
         End If
    End Sub

    C#

    private void listBoxItems_DoubleClick(object sender, EventArgs e)
    {
        if (listBoxItems.SelectedIndex >= 0)
        {
            MapiItem item = current.MapiItems[listBoxItems.SelectedIndex];
            object body = item.GetProperty(ADXMAPIPropertyTag._PR_BODY);
            if (body != null)
            {
                System.Windows.Forms.MessageBox.Show(body.ToString(), 
                  "The body of the mail item", MessageBoxButtons.OK, 
                  MessageBoxIcon.Information);
            }
        }
    }

Conclusion

That's it. Our application is ready to go. The TreeView control on the left shows the folder tree of the first message store of the profile we are logged in to. You can see the same tree in your default mail agent (it may be, for example, Microsoft Outlook). The ListBox control on the right shows all the items contained in the selected folder in the TreeView control. By clicking on an item in the right panel, you get the body (PR_BODY) of the MAPI item.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Andrei Smolin
Team Leader Add-in Express
Belarus Belarus
No Biography provided

Comments and Discussions

 
GeneralMAPI subsystem didn't initialize correctly Pinmembertxdmh719-Aug-10 5:47 
GeneralRe: MAPI subsystem didn't initialize correctly PinmemberEugene I.17-Sep-10 1:01 
QuestionHow to get an attachment from mail PinmembertheRealScarecrow18-Jul-10 5:18 
AnswerRe: How to get an attachment from mail PinmemberEugene I.19-Jul-10 6:50 
GeneralRe: How to get an attachment from mail PinmembertheRealScarecrow19-Jul-10 9:25 
AnswerRe: How to get an attachment from mail PinmemberEugene I.20-Jul-10 2:09 
QuestionSystem.BadImageFormatException was unhandled Pinmemberjohn_172614-Jul-10 7:36 
GeneralRe: System.BadImageFormatException was unhandled Pinmemberjohn_172614-Jul-10 8:09 
AnswerRe: System.BadImageFormatException was unhandled PinmemberEugene I.15-Jul-10 0:29 
Generali have a problem when i run the app. Pinmembersolbyte6-Jul-10 1:44 
GeneralRe: i have a problem when i run the app. PinmemberEugene I.15-Jul-10 0:31 
General64-bit Outlook Pinmemberdeanit11-Jan-10 5:04 
GeneralRe: 64-bit Outlook PinmemberAndrei Smolin13-Jan-10 0:19 
QuestionHow to create outlook calender folder item using vb.net code? PinmemberMember 434622729-Oct-09 19:31 
AnswerRe: How to create outlook calender folder item using vb.net code? PinmemberAndrei Smolin6-Nov-09 1:35 
QuestionSome Questions Pinmembermailtorakib23-Sep-09 16:02 
AnswerRe: Some Questions PinmemberAndrei Smolin24-Sep-09 1:55 
QuestionIs that possible to send email using MAPI Store Accessor Pinmembermeramkumar10-Jun-09 19:51 
AnswerRe: Is that possible to send email using MAPI Store Accessor PinmemberAndrei Smolin25-Jun-09 6:40 
GeneralMAPI PinmemberRajeevBhatt5-Feb-09 2:43 
GeneralRe: MAPI PinmemberAndrei Smolin5-Feb-09 9:16 
GeneralRe: MAPI Pinmemberjanjerell31-Mar-09 15:53 
GeneralRe: MAPI PinmemberAndrei Smolin1-Apr-09 1:29 
QuestionEmail contact Bug ? Pinmemberouialors16-Dec-08 6:57 
AnswerRe: Email contact Bug ? Pinmemberouialors16-Dec-08 7:16 
GeneralRe: Email contact Bug ? PinmemberAndrei Smolin25-Jun-09 7:37 
QuestionMobile6 support PinmemberDennisZweigle29-Oct-08 20:22 
AnswerRe: Mobile6 support PinmemberAndrei Smolin19-Feb-09 6:25 
QuestionHow to create a new MailItem using the mapi accessor? PinmemberRVW22-Jun-08 8:41 
AnswerRe: How to create a new MailItem using the mapi accessor? PinmemberAndrei Smolin23-Jun-08 4:22 
QuestionHow to create new OutlookTask using your add-in? Pinmembersoniko13-Feb-08 13:25 
AnswerRe: How to create new OutlookTask using your add-in? PinmemberAndrei Smolin18-Feb-08 23:43 
QuestionHow to get the attachments? PinmemberAnmum5-Feb-08 6:20 
GeneralRe: How to get the attachments? PinmemberAndrei Smolin11-Feb-08 3:31 
GeneralRe: How to get the attachments? PinmemberAnmum9-May-08 1:59 
GeneralRe: How to get the attachments? PinmemberAndrei Smolin27-May-08 4:29 
AnswerRe: How to get the attachments? PinmemberEugene I.12-Jul-10 3:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 24 Jan 2008
Article Copyright 2008 by Andrei Smolin
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid