【问题标题】:Outlook.MailItem - Unable to get To, CC & BCC from OutlookOutlook.MailItem - 无法从 Outlook 获取收件人、抄送和密件抄送
【发布时间】:2014-12-27 18:03:19
【问题描述】:

我正在尝试通过自定义发送按钮的 Application_ItemSend 事件来使用 C# 创建 Outlook 插件。

在发送电子邮件之前,我需要所有电子邮件详细信息。

当我在@my home 运行以下代码时,我会通过输入一些个人电子邮件 ID 来获得正确的结果。

但是当我在 Office Outlook 机器上运行这个类似的代码时,我得到了名字。

由于默认情况下启用了 Outlook 的检查名称代码,这将返回名字和姓氏。

我在这两个地方都使用 Outlook 2010。 Office Outlook 映射到 Office 活动目录。我的家庭前景没有映射。任何人都可以提供一个通用的解决方案,它将给我所有使用的电子邮件地址(to、cc、bcc & from),无论是否映射了活动目录。

private void ThisAddIn_Startup(object sender, System.EventArgs e)        { 
          Application.ItemSend += new           Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);  
}

void Application_ItemSend(object Item, ref bool Cancel)        {
Outlook.MailItem mail = Item as Outlook.MailItem;            
Outlook.Inspector inspector = Item as Outlook.Inspector;

System.Windows.Forms.MessageBox.Show(mail.CC);
System.Windows.Forms.MessageBox.Show(mail.BCC);

} 

【问题讨论】:

    标签: c# email outlook outlook-addin outlook-2010


    【解决方案1】:

    也许迟到了,但有人可以检查此代码(它对我有用)

        private string[] GetCCBCCFromEmail(Outlook.MailItem email)
        {
            string[] ccBCC = new string[] { "", "" };//cc y bcc
            Outlook.Recipients recipients = email.Recipients;
    
            foreach (Outlook.Recipient item in recipients)
            {
                switch (item.Type)
                {
    
                    case (int)Outlook.OlMailRecipientType.olCC:                        
                        ccBCC[0] += GetEmail(item.AddressEntry) + ";";
                        break;
                    case (int)Outlook.OlMailRecipientType.olBCC:
                        ccBCC[1] += GetEmail(item.AddressEntry) + ";";
                        break;
                }
            }
            return ccBCC;
        }
        private string GetEmail(Outlook.AddressEntry address) 
        {
            string addressStr = "";
    
            if (address.AddressEntryUserType ==
                    Outlook.OlAddressEntryUserType.
                    olExchangeUserAddressEntry
                    || address.AddressEntryUserType ==
                    Outlook.OlAddressEntryUserType.
                    olExchangeRemoteUserAddressEntry)
            {
                //Use the ExchangeUser object PrimarySMTPAddress
                Outlook.ExchangeUser exchUser =
                    address.GetExchangeUser();
                if (exchUser != null)
                {
                    addressStr = exchUser.PrimarySmtpAddress;
                }
            }
            //Get the address from externals
            if (address.AddressEntryUserType == Outlook.OlAddressEntryUserType.
                olSmtpAddressEntry)
            {
                addressStr = address.Address;
            }
    
            return addressStr;
        }
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      To/CC/BCC 属性(对应于 MAPI 中的 PR_DISPLAY_TO/CC/BCC)由存储提供程序在保存项目时更新 (MailItem.Save)。您还可以使用 MailItem.Recipeints 集合访问所有收件人。

      【讨论】:

      • 那么我应该先保存 MailItem 以获取详细信息吗?这就是你想说的吗?但我面临的问题是我得到的是名字而不是电子邮件ID......就像(sameer k)而不是sameer.k@mail.com......但在我家设置它工作正常......因为Outlook被映射到家里的 gmail...
      • 是的,您需要保存邮件才能读取收件人/抄送/密送属性。如果您需要地址,请使用我的第二个建议并遍历 MailItem.Recipients 集合。对于每个收件人对象,读取其地址、名称、类型属性。
      • 嗨..我尝试使用 To/CC/BCC 而不保存 mailItem 但从 Recipients Object 进行迭代。但是得到的结果类似于 ldap 查询.../O=SOMEVALUE/OU=SOMEVALUE/cn =Recipients/cn=SOMEVALUE in Address 属性..还有其他方法可以获取电子邮件地址吗..?
      • 这是一个非常好的 EX 类型地址(相对于 SMTP)。如果您需要默认 SMTP 地址,请使用 Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress。准备好处理空值/错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 2012-04-10
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多