65.9K
CodeProject 正在变化。 阅读更多。
Home

在 .NET 中使用 MAPI 属性和事件,以及 MAPI Store Accessor

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.83/5 (3投票s)

2008年1月24日

CPOL

3分钟阅读

viewsIcon

104776

downloadIcon

1478

一个简单的 WinForms 应用程序,用于显示您邮件配置文件中的 MAPI 文件夹和项。

A running application

A PR_BODY value of the selected item

引言

本文介绍了编写使用 Extended MAPI 的简单应用程序的基础知识。本文的主要目的是向开发人员展示如何使用 Extended MAPI 来增强他们的应用程序。要使此应用程序正常工作,您需要下载并安装一个免费组件,即MAPI Store Accessor for .NET

MAPI Store Accessor

MAPI Store Accessor 是一个 .NET 组件,允许您订阅诸如 OnNewMailOnObjectChangedOnObjectCopied 等事件。它还允许您访问 MAPI 存储、文件夹、项和附件。您会发现使用 MAPI Store Accessor 可以有效地隐藏 MAPI 的复杂性。

目前,MAPI Store Accessor for .NET 支持

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

AddinExpress.MAPI 库中的基本类是

  • Store - 表示配置文件中的一个存储
  • Folder - 表示存储中的一个文件夹
  • MapiItem - 表示文件夹中的一个项
  • HiddenItem - 表示文件夹中的一个隐藏项
  • Attachment - 表示一个附件

还有相应的集合,如 StoresFolders 等。要开始使用这些类,当组件连接到 MAPI 子系统时,您应该从该组件获取存储的集合。

第一步

要开始使用 MAPI Store Accessor,请在工具箱窗口中找到它并将其拖放到 Windows 窗体上,或者手动声明代码中的组件。

您需要做两件重要的事情

  • 调用 Initialize
  • 调用 LogOff

它们就像开始和停止命令。当调用带有布尔参数的 Initialize 时,该组件会连接到默认配置文件。您的应用程序根据传递给方法的​​值来创建或连接到现有的 MAPI 会话。如果要创建新会话,请传递 True。如果需要连接到现有会话,请传递 False。当您传递 False 时,请确保会话存在。Initialize 方法的另一个重载接受配置文件名和密码。

编码

在适当的地方添加 InitializeLogOff。我分别在窗体的 LoadFormClosing 事件处理程序中进行。

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();
}

现在,我们准备为应用程序添加业务逻辑。我们需要一个 TreeView 和一个 ListBox。将它们拖放到窗体上。根据您的喜好设置适当的属性。最后,添加以下事件处理程序

  • TreeView 控件的 AfterSelect 事件处理程序
  • 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());
            }
        }
    }
  • ListBox 控件的 DoubleClick 事件处理程序
  • 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);
            }
        }
    }

结论

就是这样。我们的应用程序已准备就绪。左侧的 TreeView 控件显示我们已登录配置文件的第一个邮件存储的文件夹树。您可以在默认邮件代理(例如 Microsoft Outlook)中看到相同的树。右侧的 ListBox 控件显示 TreeView 控件中选定文件夹包含的所有项。通过单击右侧面板中的项,您可以获取 MAPI 项的正文 (PR_BODY)。

© . All rights reserved.