How to: Get the E-mail Address of a Contact Item

How to: Get the E-mail Address of a Contact Item

This topic shows how to obtain the value of a named property that represents the e-mail address of an Outlook Contact item.

You can associate up to three e-mail addresses with a Contact item in Outlook. Each e-mail address corresponds to a property of the Outlook ContactItem object in the Outlook object model. Internal to Outlook, the e-mail address also corresponds to a MAPI named property. For example, the first e-mail address of a contact corresponds to the Email1Address property of the ContactItem in the Outlook object model, and the Outlook internal named property dispidEmailEmailAddress.

To obtain the value of an e-mail address of a contact item, you can use the PropertyAccessor object of the Outlook object model, or first use IMAPIProp::GetIDsFromNames to obtain the property tag of the named property, and then specify this property tag in IMAPIProp::GetProps to get the value. When calling IMAPIProp::GetIDsFromNames, specify the appropriate values for the MAPINAMEID structure pointed at by the input parameter lppPropNames. The following code sample shows how to obtain the first e-mail address of a specified contact, lpContact, using GetIDsFromNames and GetProps.

  HRESULT HrGetEmail1(LPMESSAGE lpContact)
{
    HRESULT hRes = S_OK;
    LPSPropTagArray lpNamedPropTags = NULL;
    MAPINAMEID NamedID = {0};
    LPMAPINAMEID lpNamedID = &NamedID;
    NamedID.lpguid = (LPGUID)&PSETID_Address;
    NamedID.ulKind = MNID_ID;
    NamedID.Kind.lID = dispidEmailEmailAddress;
hRes = lpContact->GetIDsFromNames(
       1, 
       &lpNamedID, 
       NULL, 
       &lpNamedPropTags);

if (SUCCEEDED(hRes) && lpNamedPropTags)
{
    SPropTagArray sPropTagArray;
			
    sPropTagArray.cValues = 1;
    sPropTagArray.aulPropTag[0] = CHANGE_PROP_TYPE(lpNamedPropTags->aulPropTag[0],PT_STRING8);
    LPSPropValue lpProps = NULL;
    ULONG cProps = 0;

    hRes = lpContact->GetProps(
           &sPropTagArray,
           NULL,
           &cProps,
           &lpProps);
    if (SUCCEEDED(hRes) && 
        1 == cProps && 
        lpProps && 
        PT_STRING8 == PROP_TYPE(lpProps[0].ulPropTag) &&
        lpProps[0].Value.lpszA)
    {
        printf("Email address 1 = \"%s\"\n",lpProps[0].Value.lpszA);
    }
    MAPIFreeBuffer(lpProps);
    MAPIFreeBuffer(lpNamedPropTags);
 }
 return hRes;

}