【问题标题】:How to retrieve CC email address from MS outlook 2010?如何从 MS Outlook 2010 中检索 CC 电子邮件地址?
【发布时间】:2013-08-20 18:51:42
【问题描述】:

我正在使用以下代码从 MS Outlook 2010 中检索不同的邮件参数。但我无法获取 CC 电子邮件地址。 MailItem 类的 CC 属性返回的是名称,而不是电子邮件地址。

            NameSpace _nameSpace;
            ApplicationClass _app;
            _app = new ApplicationClass();
            _nameSpace = _app.GetNamespace("MAPI");
            object o = _nameSpace.GetItemFromID(EntryIDCollection);
            MailItem Item = (MailItem)o;
            string HTMLbpdyTest = Item.HTMLBody;
            CreationTime = Convert.ToString(Item.CreationTime);
            strEmailSenderEmailIdMAPI = Convert.ToString(Item.SenderEmailAddress);
            strEmailSenderName = Item.SenderName;
            Subject = Item.Subject;
            string CCEmailAddress = Item.CC;

请建议,我怎样才能获得抄送电子邮件地址?

【问题讨论】:

    标签: c# outlook outlook-2010


    【解决方案1】:
    public static int ConnectToOutlook()
    {
        try
        {
            Outlook.Application oApp = new Outlook.Application();
            Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
            oNS.Logon(Missing.Value, Missing.Value, false, true);                        
            Outlook.MAPIFolder oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Parent;                              
            List<string> ccRecipient = new List<string>();
            foreach (MAPIFolder folder in oInbox.Folders)
            {
                if (folder.FullFolderPath.Contains("Inbox"))
                {
                    foreach (MAPIFolder subFolder in folder.Folders)
                    {
                        try
                        {
                            if (subFolder.FullFolderPath.Contains("Folder Name Inside Inbox"))
                            {
                                foreach (object folderItems in subFolder.Items)
                                {
                                    if (folderItems is Outlook.MailItem)
                                    {
                                        Outlook.MailItem email_Msg = (Outlook.MailItem)folderItems;
                                        Console.WriteLine("Subject=>" + email_Msg.Subject);
                                        //Console.WriteLine("From=>" + email_Msg.SenderEmailAddress);
                                        //Console.WriteLine("Cc=>" + email_Msg.CC);
                                        //Console.WriteLine("Recipients=>" + email_Msg.Recipients[1].Address);
                                        foreach (Recipient recipient in email_Msg.Recipients)
                                        {
                                            if ((OlMailRecipientType)recipient.Type == OlMailRecipientType.olCC)
                                            {
                                                Console.WriteLine("Cc=>" + recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress);                                                                                                      
                                            }
    
                                        }                                           
                                    }
                                }
                            }
                        }
                        catch (System.Exception error)
                        {
                            Console.WriteLine();
                            Console.WriteLine(error.Message);
                        }
                    }
                }
            }               
        }
        catch (System.Exception e)
        {
            Console.WriteLine("{0} Exception caught: ", e);
        }
        return 0;
    }
    

    【讨论】:

      【解决方案2】:

      循环遍历MailItem.Recipients 集合并为每个Recipient 对象检查其Type 属性; olCC 是你想要的。然后您可以读取Recipient.Address 属性。

      编辑:我的头顶。

      foreach (Recipient recip in Item.Recipients)
      {
        if (recip.Type == OlMailRecipientType.olCC)
        {
          if (CCEmailAddress.length > 0) CCEmailAddress += ";";
          CCEmailAddress += recip.Address;
        }
      }
      

      【讨论】:

      • 你有这方面的示例代码吗?我正在尝试但无法实现。
      • if (recip.Type == OlMailRecipientType.olCC) 给出错误 - 运算符“==”不能应用于“int”和“Microsoft.Office.Interop.Outlook.OlMailRecipientType”类型的操作数
      • 您需要将左侧转换为 OlMailRecipientType 或将右侧转换为 int。或者,由于 olCC 为 2,只需将 Recipient.Type 与 2 进行比较。
      • thnx.. 但现在我收到这样的电子邮件地址 - /O=EXG5/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=Test88067
      • 这是一个完全有效的 EX 地址。如果您想要 SMTP 地址,请使用 Recipient.AddressEntry.GetExchangeUser.PrmarySmtpAddress。
      【解决方案3】:

      我受到@Dmitry 的回答的启发,我自己尝试了一些方法来让这些代码行解决我的问题,并为我提供给定邮件项目中存在的抄送地址数组。

      public string[] GetCCAddress(MailItem mailItem)
          {
              string email;
              Outlook.ExchangeUser exUser;
              List <string> ccEmailAddressList = new List<string>();
              foreach (Recipient recip in mailItem.Recipients)
              {
                  if ((OlMailRecipientType)recip.Type == OlMailRecipientType.olCC)
                  {
                          email=recip.Address;
                          if (!email.Contains("@"))
                          {
                              exUser = recip.AddressEntry.GetExchangeUser();
                              email = exUser.PrimarySmtpAddress;
                          }
                          ccEmailAddressList.Add(email);
      
                  }
              }
      

      此声明if (!email.Contains("@")) 是为了避免在实际电子邮件地址上调用exUser.PrimarySmtpAddress,并将其限制为“/O=EXG5/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=Test88067”等条目

      【讨论】:

      • 您最好使用 Recipient.PropertyAccessor.GetProperty 检索 PR_ADDRTYPE 属性(DASL 名称 http://schemas.microsoft.com/mapi/proptag/0x3002001F)并检查它是否为“EX”。
      • 我听从了你的建议。谢谢。
      • 您还可以检查 recip.AddressEntry 和 AddressEntry.GetExchangeUser 是否不为空。
      • cc字段中已经是EX类型怎么可能为null?
      • 1.邮件已从 Exchange 邮箱复制到 PST 存储,并且该 PST 存储在没有原始 Exchange 服务器的配置文件中打开。 2. 服务器或网络已关闭,无法联系 GAL。
      【解决方案4】:

      试试

      Item.CC.Address
      

      ((MailAddress)Item.CC).Address
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-02
        • 1970-01-01
        • 2020-09-28
        • 1970-01-01
        • 1970-01-01
        • 2010-10-22
        相关资源
        最近更新 更多