如何从MS Outlook 2010中检索CC电子邮件地址?

时间:2013-08-19 03:19:23

标签: c# outlook outlook-2010

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

            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;

请建议,我如何获取CC电子邮件地址?

4 个答案:

答案 0 :(得分:3)

循环遍历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;
  }
}

答案 1 :(得分:1)

我受到@Dmitry的回答的启发,并且我自己尝试了一些事情来让这些代码修复我的问题并给出一系列给定邮件中存在的cc-ed地址。

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"

答案 2 :(得分:0)

尝试

Item.CC.Address

((MailAddress)Item.CC).Address

答案 3 :(得分:0)

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