none
Get FirstName and LastName From Alias Name RRS feed

  • Question

  • Hi All,

    I just want to create a windows Application (C#), the requirement is it contains 1 text box, 1 label & 1 button.

    When I enter the Alias name in the text box and press the button the Name should be retrieved from the outlook contacts based on the alias name and should be displayed in the label.

     

    Can any one help.. Thanks in Advance

    Tuesday, March 15, 2011 6:47 AM

Answers

  • If the alias is something that can be resolved in the user interface you can create a recipient using the alias and get an address entry object from that and from there a name.

    For example, if I use a "kens" alias it will resolve to my Exchange email address.

    You can use code like this, where olApp is an Outlook.Application reference:

    Outlook.NameSpace ns = olApp.GetNameSpace("MAPI");

    Outlook.Recipient recip = ns.CreateRecip("kens");

    recip.Resolve();

    Outlook.AddressEntry ae = recip.AddressEntry;

    string name = ae.Name;

     


    Ken Slovak MVP - Outlook
    • Marked as answer by ChitturiRakhi Thursday, March 17, 2011 5:51 AM
    Tuesday, March 15, 2011 1:17 PM

All replies

  • If the alias is something that can be resolved in the user interface you can create a recipient using the alias and get an address entry object from that and from there a name.

    For example, if I use a "kens" alias it will resolve to my Exchange email address.

    You can use code like this, where olApp is an Outlook.Application reference:

    Outlook.NameSpace ns = olApp.GetNameSpace("MAPI");

    Outlook.Recipient recip = ns.CreateRecip("kens");

    recip.Resolve();

    Outlook.AddressEntry ae = recip.AddressEntry;

    string name = ae.Name;

     


    Ken Slovak MVP - Outlook
    • Marked as answer by ChitturiRakhi Thursday, March 17, 2011 5:51 AM
    Tuesday, March 15, 2011 1:17 PM
  • Thanks Ken, it worked. Now I am able to get name.

     

    Thank you very Much.. May God Bless You. Happy St.Patricks Day.

    Thursday, March 17, 2011 5:52 AM
  • Ken,

    What if i want get the email address of the user based on alias.

    And also if i want to get the first & last names what should i do??? :-(

    Thursday, March 17, 2011 6:06 AM
  • The Object Browser is your friend.

    The AddressEntry object you get in the code above has an Address property.

    If the user has an Exchange email address the Address property wll be in EX format however, and not in SMTP format. In that case, you can use the AddressEntry.GetExchangeUser() function to get an ExchangeUser object and get the PrimarySmtpAddress property, which will return the SMTP address for that EX users.

    If the user is not on Exchange the Address property will be the SMTP address.

    For name you can parse the AddressEntry.Name property by spaces for example. If the AddressEntry is for a contact item in Outlook you could use AddressEntry.GetContact() and in that case you can directly get FirstName and LastName from the ContactItem.

     


    Ken Slovak MVP - Outlook
    Thursday, March 17, 2011 1:42 PM
  •  

    Hi Ken,

    I am using AddressEntry.GetContact() to get "BusinessFaxNumber" or "LastName" but its showing "Access Violation Exception".

    Please suggest me what I need to provide to get it done.


    Thanks,

    Bhushan Nankar 

     

    Thursday, March 31, 2011 7:10 AM
  • Is there a contact for that AddressEntry? If not, you obviously can't retrieve a contact or its properties from the AddressEntry.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "NankarB" <=?utf-8?B?TmFua2FyQg==?=> wrote in message news:06d58454-036c-45f8-a660-d45e77d8ea6a...

     

    Hi Ken,

    I am using AddressEntry.GetContact() to get "BusinessFaxNumber" or "LastName" but its showing "Access Violation Exception".

    Please suggest me what I need to provide to get it done.


    Thanks,

    Bhushan Nankar 

     


    Ken Slovak MVP - Outlook
    Thursday, March 31, 2011 1:45 PM
  • Thanks Ken for immediate reply but below is my problem description. 

    I want to get Contact details from Global Address List and the code I have witten is :

     private void LoadListBox()

            {

                // Create Outlook application.

                Outlook.Application oApp = new Outlook.Application();

     

                // Get Mapi NameSpace and Logon.

                Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

                oNS.Logon(Missing.Value, Missing.Value, false, true);

               

                // Get Global Address List.

                Outlook.AddressLists oDLs = oNS.AddressLists;

                Outlook.AddressList oGal = oDLs["Global Address List"];

     

                //// Get a specific distribution list.

                //// TODO: Replace the distribution list with a distribution list that is available to you.

                string sDL = "Testing";

                Outlook.AddressEntries oEntries = oGal.AddressEntries;

                //// No filter available to AddressEntries

                //Outlook.AddressEntry oDL = oEntries[sDL];

                Outlook.AddressEntry oEntry = default(Outlook.AddressEntry);

                int i = 0;

          

                for (i = 1; i <= oGal.AddressEntries.Count; i++)

                {

                    oEntry = oGal.AddressEntries[i];

     

                    if (oEntry.GetContact().Email1Address.Equals("Contact Name"))

                    {

                        string BusinessFaxNumber = oEntry.GetContact().BusinessFaxNumber;

                    }

     

                }

     

                // Log off.

                oNS.Logoff();

     

                // Clean up.

                oApp = null;

                oNS = null;

                oDLs = null;

                oGal = null;

               

            }

     

    It contains hundreds of entries in Global Address List

    But at the line of “if (oEntry.GetContact().Email1Address.Equals("Contact Name"))”

     

    Its showing “Access Violation Exception” with description “Attempted to read or write protected memory. This is often an indication that other memory is corrupt.”

    Friday, April 1, 2011 8:55 AM
  • The GAL and its AddressEntry objects are not contacts. Contacts are those items you find in Contacts folders. If those GAL entries have not been also created as contact items what you want cannot be done.
     
    The AddressEntry properties don't expose what you want.
     
    You would either have to use LDAP code to query the Active Directory, or use EWS (Exchange Web Services) code. You would have to reiview how to do what you want using those technologies. There probably isn't much if any expertise in those things here, so you might want to post in http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/threads
     and ask about them there.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "NankarB" <=?utf-8?B?TmFua2FyQg==?=> wrote in message news:d36c58be-afd0-4968-867b-ab6360a2fe86...

    Thanks Ken for immediate reply but below is my problem description. 

    I want to get Contact details from Global Address List and the code I have witten is :

     private void LoadListBox()

            {

                // Create Outlook application.

                Outlook.Application oApp = new Outlook.Application();

     

                // Get Mapi NameSpace and Logon.

                Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

                oNS.Logon(Missing.Value, Missing.Value, false, true);

               

                // Get Global Address List.

                Outlook.AddressLists oDLs = oNS.AddressLists;

                Outlook.AddressList oGal = oDLs["Global Address List"];

     

                //// Get a specific distribution list.

                //// TODO: Replace the distribution list with a distribution list that is available to you.

                string sDL = "Testing";

                Outlook.AddressEntries oEntries = oGal.AddressEntries;

                //// No filter available to AddressEntries

                //Outlook.AddressEntry oDL = oEntries[sDL];

                Outlook.AddressEntry oEntry = default(Outlook.AddressEntry);

                int i = 0;

          

                for (i = 1; i <= oGal.AddressEntries.Count; i++)

                {

                    oEntry = oGal.AddressEntries[i];

     

                    if (oEntry.GetContact().Email1Address.Equals("Contact Name"))

                    {

                        string BusinessFaxNumber = oEntry.GetContact().BusinessFaxNumber;

                    }

     

                }

     

                // Log off.

                oNS.Logoff();

     

                // Clean up.

                oApp = null;

                oNS = null;

                oDLs = null;

                oGal = null;

               

            }

     

    It contains hundreds of entries in Global Address List

    But at the line of “if (oEntry.GetContact().Email1Address.Equals("Contact Name"))”

     

    Its showing “Access Violation Exception” with description “Attempted to read or write protected memory. This is often an indication that other memory is corrupt.”


    Ken Slovak MVP - Outlook
    Friday, April 1, 2011 1:06 PM
  • I accidentally stepped on this forum and found out what i required exactly..

    Thank you Ken

     
    Tuesday, August 26, 2014 7:14 AM