1

我将访问 Outlook MAPI 文件夹并获取邮件地址。这是我的方法

 public static string GetSenderEmailAddress(Microsoft.Office.Interop.Outlook.MailItem mapiObject)
    {
        Microsoft.Office.Interop.Outlook.PropertyAccessor oPA;
        string propName = "http://schemas.microsoft.com/mapi/proptag/0x0065001F";
        oPA = mapiObject.PropertyAccessor;
        string email = oPA.GetProperty(propName).ToString();
        return email;
    }

当调用按钮单击事件时,我需要触发该方法并检索邮件地址。

按钮点击事件在这里。

      private void button3_Click(object sender, RibbonControlEventArgs e)
        {

 string mailadd =  ThisAddIn.GetSenderEmailAddress(Microsoft.Office.Interop.Outlook.MailItem);
        System.Windows.Forms.MessageBox.Show(mailadd);

}

错误在这里

Microsoft.Office.Interop.Outlook.MailItem 是在给定上下文中无效的“类型”

这是我的第一个插件,有谁知道如何实现这个结果?

4

1 回答 1

0

您可以使用RibbonControlEventArgs来访问Context将为您提供MailItem实例的 。

private Outlook.MailItem GetMailItem(RibbonControlEventArgs e)
{
    // Inspector Window
    if (e.Control.Context is Outlook.Inspector)
    {
        Outlook.Inspector inspector = e.Control.Context as Outlook.Inspector;
        if (inspector == null) return null;
        if (inspector.CurrentItem is Outlook.MailItem)
            return inspector.CurrentItem as Outlook.MailItem;
    }
    // Explorer Window 
    if (e.Control.Context is Outlook.Explorer)
    {
        Outlook.Explorer explorer = e.Control.Context as Outlook.Explorer;
        if (explorer == null) return null; 
        Outlook.Selection selectedItems = explorer.Selection;
        if (selectedItems.Count != 1)  return null;  
        if (selectedItems[1] is Outlook.MailItem)
            return selectedItems[1] as Outlook.MailItem;
    }     
    return null;
}

您可以添加此方法,然后像这样使用它...

string mailAddress = string.Empty;
Outlook.MailItem mailItem = GetMailItem(e);
if (mailItem != null)
    mailAddress =  ThisAddIn.GetSenderEmailAddress(mailItem);
于 2013-02-07T02:11:21.110 回答