1

我正在使用以下代码从 MS Outlook 2010 中检索不同的邮件参数。但我无法获取抄送电子邮件地址。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;

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

4

4 回答 4

5

遍历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;
  }
}
于 2013-08-19T03:53:43.490 回答
1

我受到@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”之类的条目

于 2015-08-04T22:59:48.713 回答
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;
}
于 2021-05-19T17:00:27.020 回答
-1

尝试

Item.CC.Address

或者

((MailAddress)Item.CC).Address
于 2013-08-19T03:49:22.113 回答