简体   繁体   中英

How to retrieve CC email address from MS outlook 2010?

I am using below code to retrieve the different mail parameter from MS outlook 2010. but I am not able to get CC email address. CC property of MailItem class is returning Name, not email address.

            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;

Please suggest, how can I get CC email addresses?

Loop through the MailItem.Recipients collection and for each Recipient object check its Type property; olCC is what you want. You can then read the Recipient.Address property.

EDIT: Off the top of my head.

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

I was inspired by @Dmitry's answer and tried a few things on my own to have these lines of code fix my problems and give me an array of the cc-ed addresses that are present in a given mail item.

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

            }
        }

This statement if (!email.Contains("@")) is to avoid calling exUser.PrimarySmtpAddress on an actual email address and restrict that to entries such as " /O=EXG5/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=Test88067"

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

Try

Item.CC.Address

or

((MailAddress)Item.CC).Address

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM