简体   繁体   中英

VSTO outlook mailItem Application_ItemSend to catach a contactItem from a recipient of Active Directory

how to get ContactItem from Recipients property by Active directory. I had trid the code and get the ContactItem seccsussful when the Recipients is saved in local contact. how to get the ContactItem when a Recipients was only exists only on Active directory.

public void Application_ItemSend(object mail, ref bool Cancel) {

        Outlook.MAPIFolder fldContacts = (Outlook.MAPIFolder)Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
            for (int i = 1; i < mail.Recipients.Count + 1; i++)
            {
                Outlook.Recipient r = mail.Recipients.Item(i);
                if (!r.Resolved) r.Resolve();
                if (r.Resolved)
                {
                    Outlook.ContactItem ci = (fldContacts.Items.Find("[Email1Address] = '" + r.Address + "'") as Outlook.ContactItem);
                    if (ci != null)
                    {
                        //to get the Department of Recipient
                        string DepartmentName = ci.Department;
                    }
                }
            }

    }

You could refer to the below code:

bool resolved;
        Microsoft.Office.Interop.Outlook.Application olApplication = new Microsoft.Office.Interop.Outlook.Application();

        // get nameSpace and logon.
        Microsoft.Office.Interop.Outlook.NameSpace olNameSpace = olApplication.GetNamespace("mapi");
        olNameSpace.Logon("Outlook", "", false, true);

        // get the Calender items
        Microsoft.Office.Interop.Outlook.MAPIFolder _olCalender = olNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);

        // Get the Items (Appointments) collection from the Calendar folder.
        Microsoft.Office.Interop.Outlook.Items oItems = _olCalender.Items;



        foreach (object o in oItems)
        {

            if (o is Microsoft.Office.Interop.Outlook.AppointmentItem)
            {
                Microsoft.Office.Interop.Outlook.Recipients recipients = ((Microsoft.Office.Interop.Outlook.AppointmentItem)o).Recipients;
                foreach (Microsoft.Office.Interop.Outlook.Recipient rec in recipients)
                {
                    resolved = rec.Resolve();
                    if (resolved)
                    {
                        Microsoft.Office.Interop.Outlook.ContactItem contactItem = rec.AddressEntry.GetContact();

                    }
                }

            }
        }

For more information, Please refer to this link:

C# Outlook get CompanyName property from Recipient

just change the code like the folowing:

Outlook.ContactItem ci = (fldContacts.Items.Find("[Email1Address] = '" + r.Address + "'") as Outlook.ContactItem);

change to ExchangeUser contactItem = recip.AddressEntry.GetExchangeUser();

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