在Outlook VSTO +阅读邮件提示中获取收件人的电子邮件地址

时间:2016-08-31 13:35:10

标签: c# outlook vsto exchange-server

我目前遇到的问题是使用VSTO为2010年创建的Outlook加载项,我开发时确定电子邮件地址是位于组织内部还是位于组织外部。

在发送电子邮件时,我正在使用以下MAPI属性来阅读收件人的电子邮件地址。其中addr是AddressEntry对象。

private void AdjustWidthComboBox(ComboBox comboBox)
{
    int width = comboBox.DropDownWidth;
    using (Graphics g = comboBox.CreateGraphics())
    {
        Font font = comboBox.Font;
        int vertScrollBarWidth =
            (comboBox.Items.Count > comboBox.MaxDropDownItems)
            ? SystemInformation.VerticalScrollBarWidth : 0;

        foreach (object item in comboBox.Items)
        {
            string valueToMeasure = comboBox.GetItemText(item);
            int newWidth = (int)g.MeasureString(valueToMeasure, font).Width + vertScrollBarWidth;
            if (width < newWidth)
            {
                width = newWidth;
            }
        }
    }

    comboBox.DropDownWidth = width;
}

private void cbSample_DropDown(object sender, EventArgs e)
{
    AdjustWidthComboBox(sender as ComboBox);
}

这适用于交换联系人和本地联系人。但是,如果您创建本地联系人并从全局通讯簿中选择电子邮件地址,则该特定联系人中不存在此MAPI属性。我只需要知道什么是最好的方法来查找联系人(电子邮件地址)是在组织内部还是在组织外部(此方法也会降低性能,组织中有人会向500多个人发送电子邮件用户一次通过上面的MAPI属性处理内部/外部电子邮件扫描需要10多分钟。我知道outlook已经在“New Email”窗口中显示与MailTip相同的信息。所以我的问题是

  1. 有没有办法点击邮件提示并从中读取信息?

  2. 如果没有最好的方式来查找联系电子邮件地址是否在组织内部/外部(考虑到性能)

2 个答案:

答案 0 :(得分:0)

Outlook对象模型不提供阅读邮件提示的任何内容。

尝试使用以下代码:

private string GetSenderSMTPAddress(Outlook.MailItem mail)
{
    string PR_SMTP_ADDRESS =
       @"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
    if (mail == null)
    {
        throw new ArgumentNullException();
    }
    if (mail.SenderEmailType == "EX")
    {
       Outlook.AddressEntry sender =
          mail.Sender;
       if (sender != null)
       {
           //Now we have an AddressEntry representing the Sender
           if (sender.AddressEntryUserType ==
               Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry
               || sender.AddressEntryUserType ==                    Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
           {
               //Use the ExchangeUser object PrimarySMTPAddress
               Outlook.ExchangeUser exchUser =
                   sender.GetExchangeUser();
               if (exchUser != null)
               {
                   return exchUser.PrimarySmtpAddress;
               }
               else
               {
                   return null;
               }
           }
           else
           {
               return sender.PropertyAccessor.GetProperty(
                   PR_SMTP_ADDRESS) as string;
           }
       }
       else
       {
           return null;
       }
   }
   else
   {
       return mail.SenderEmailAddress;
   }
}

How to: Get the SMTP Address of the Sender of a Mail Item。您也可以找到有用的HowTo: Convert Exchange-based email address into SMTP email address文章。

答案 1 :(得分:0)

Mail tips can be accessed using EWS only - see https://msdn.microsoft.com/en-us/library/office/dd877060%28v=exchg.150%29.aspx?f=255&MSPPError=-2147217396

If using Redemption is an option, it exposes RDOAddressEntry.GetMailtips method that returns RDOMailTips object.

相关问题