How to: Programmatically Search for an E-Mail Address in Contacts

 

This example searches a contact folder for contacts that have the domain name example.com in their e-mail addresses.

Applies to: The information in this topic applies to VSTO add-in projects for Outlook. For more information, see Features Available by Office Application and Project Type.

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            SearchforEmail("example.com");
        }

        private void SearchforEmail(string partialAddress)
        {
            string contactMessage = string.Empty;
            Outlook.MAPIFolder contacts = (Outlook.MAPIFolder)
                this.Application.ActiveExplorer().Session.GetDefaultFolder
                 (Outlook.OlDefaultFolders.olFolderContacts);
            foreach (Outlook.ContactItem foundContact in contacts.Items)
            {
                if (foundContact.Email1Address != null)
                {
                    if (foundContact.Email1Address.Contains(partialAddress))
                    {
                        contactMessage = contactMessage + "New contact"
                        + foundContact.FirstName + " " + foundContact.LastName
                        + " Email Address is " + foundContact.Email1Address +
                        ". \n";
                    }
                }
            }
            if (!(contactMessage.Length > 0))
            {
                contactMessage = "No Contacts were found.";
            }
            MessageBox.Show(contactMessage);
        }

This example requires:

  • Contacts that have the domain name example.com in their e-mail addresses (for example, somebody@example.com), and that have first names and last names.

Working with Contact Items
How to: Programmatically Send E-Mail Programmatically
How to: Programmatically Access Outlook Contacts
How to: Programmatically Add an Entry to Outlook Contacts

Show: