How To Retrieve Alternate E-mail Addresses Using Extended MAPI (248357)



The information in this article applies to:

  • Microsoft Extended Messaging Application Programming Interface (MAPI)

This article was previously published under Q248357

SUMMARY

This article contains an Extended MAPI code sample that demonstrates how to access the PR_EMS_AB_PROXY_ADDRESSES property of the Mail-Recipient object. This multi-valued property contains the foreign system e-mail addresses (alternate e-mail addresses).

MORE INFORMATION

Microsoft Exchange Server supports the following types of addresses:
  • Exchange
  • Microsoft Mail
  • Mac Mail
  • X.400
  • Internet(SMTP)
  • Lotus cc:Mail
  • Custom
The PR_EMAIL_ADDRESS property of the Mail-Recipient object returns the Exchange (EX) type e-mail address by default. To retrieve the other addresses, you can use the PR_EMS_AB_PROXY_ADDRESSES property.

Sample Code

This code needs to be linked with Mapi32.lib.
#include <stdio.h>
#include "mapiutil.h"
#include "emsabtag.h"

void main()
{
	HRESULT			hr = S_OK;
	LPMAPISESSION		lpSession = NULL;
	LPADRBOOK			lpAddrbk = NULL;

	ULONG			cbEID = 0L;
	LPBYTE			lpEID = NULL;
	LPADRLIST			pAdrList = NULL;

	SBinary			sBin;
	ULONG			cValues = 0;
	ULONG			ulObjType = NULL;
	LPMAILUSER		pMailUser = NULL;
	LPSPropValue		lpPropValue = NULL;
	ULONG			i, j;

	SizedSPropTagArray(1, sptOneItem) = {1, PR_EMS_AB_PROXY_ADDRESSES};

	// replace with a user's alias or display name
	char* lpszDisplayName = "Alias";

	// Initialize MAPI Subsystem
	hr = MAPIInitialize(NULL);
	if (S_OK != hr) return;

	// Logon to MAPI
	hr = MAPILogonEx(0,
			NULL,
			NULL,
			MAPI_LOGON_UI | MAPI_NEW_SESSION,
			&lpSession);
	if (FAILED(hr)) goto UnInit;

	hr = lpSession->OpenAddressBook(0,NULL,0,&lpAddrbk);
	if (FAILED(hr)) goto Cleanup0;

	// resolve name
	// Allocate memory for new SRowSet structure.
	hr = MAPIAllocateBuffer(CbNewSRowSet(1),(LPVOID*) &pAdrList);
	if (FAILED(hr)) goto Cleanup0;
		
	// Zero out allocated memory.
	ZeroMemory ( pAdrList, CbNewSRowSet(1));

	hr = MAPIAllocateBuffer(7 * sizeof(SPropValue),
			(LPVOID FAR *)&(pAdrList->aEntries[0].rgPropVals));
	if (FAILED(hr)) goto Cleanup;

	// Zero out allocated memory.
	ZeroMemory ( pAdrList -> aEntries[0].rgPropVals,
			7 * sizeof(SPropValue) );

	// How many recipients to resolve.
	pAdrList->cEntries = 1;
	pAdrList->aEntries[0].cValues = 2L;

	pAdrList->aEntries[0].rgPropVals[0].ulPropTag = PR_DISPLAY_NAME;
	pAdrList->aEntries[0].rgPropVals[0].Value.lpszA =  lpszDisplayName;
	pAdrList->aEntries[0].rgPropVals[1].ulPropTag = PR_ADDRTYPE;
	pAdrList->aEntries[0].rgPropVals[1].Value.lpszA = "SMTP";

	hr = lpAddrbk -> ResolveName (0L, 0L, NULL, pAdrList );
	if (FAILED(hr)) goto Cleanup;
   
	for (i = 0; i < 7; ++i)
	{
	   // look for entry id
	   if (pAdrList->aEntries[0].rgPropVals[i].ulPropTag == PR_ENTRYID) 
	   {
			printf("EntryID: %u\n", pAdrList->aEntries[0].rgPropVals[i].Value.ul);
			
			sBin.cb = pAdrList->aEntries[0].rgPropVals[i].Value.bin.cb;
			sBin.lpb = pAdrList->aEntries[0].rgPropVals[i].Value.bin.lpb;

			hr = lpAddrbk->OpenEntry(sBin.cb,
						(LPENTRYID)sBin.lpb,
						NULL, 
						0,
						&ulObjType,
						(LPUNKNOWN*)&pMailUser);
			if (FAILED(hr)) goto Cleanup;

			hr = pMailUser->GetProps((LPSPropTagArray)&sptOneItem,
		                                 0, 
		                                 &cValues,
		                                 &lpPropValue);
			
			if (hr & MAPI_W_ERRORS_RETURNED)
			{
				printf("Warning in GetProps...\n");
				goto Cleanup;
			}

			if (HR_FAILED(hr))
			{
				printf("GetProps failed...\n");
				goto Cleanup;
			}
			// loop through the proxy multivalue property
			for (j = 0; j < lpPropValue->Value.MVszA.cValues; j++)
			{
				printf("%s\n", lpPropValue->Value.MVszA.lppszA[j]);
			}

	   }
	}

Cleanup:
	MAPIFreeBuffer((LPVOID)pAdrList);
Cleanup0:
	if (pMailUser) pMailUser->Release();
	if (lpAddrbk) lpAddrbk->Release();
	lpSession->Logoff(0, 0, 0);
	if (lpSession) lpSession->Release();
UnInit:
	MAPIUninitialize();
}
				

REFERENCES

For information on alternate e-mail addresses supported by Microsoft Exchange Server and how to retrieve the PR_EMS_AB_PROXY_ADDRESSES property using Collaboration Data Objects (CDO), please refer to the following article in the Knowledge Base:

196507 How To Retrieve Alternate E-mail Addresses Using CDO


Modification Type:MinorLast Reviewed:8/25/2005
Keywords:kbhowto kbMsg KB248357