HOWTO: Create a Personal Address Book Distribution List in VC++
ID: Q225106
|
The information in this article applies to:
-
Extended Messaging Application Programming Interface (MAPI), version 1.0
SUMMARY
The code sample provided in this article demonstrates how to create a
distribution list in the Personal Address Book and add Simple Mail Transfer Protocol (SMTP) address entries to the distribution list.
Sample Code
This code needs to be linked with Mapi32.lib.
#define INITGUID
#define USES_IID_IDistList
#include <mapiutil.h>
#include "mapidefs.h"
#include "MAPIGUID.H"
void AddEntryToDisList(LPADRBOOK lpAddrbk,
LPMAPIPROP DisList,
LPTSTR lpszDisplayName,
LPTSTR lpszAddress,
LPTSTR lpszAddrType,
LPTSTR lpPhone);
void main()
{
HRESULT hr = S_OK;
LPMAPISESSION lpSession = NULL;
LPADRBOOK lpAddrbk = NULL;
ULONG ulObjType = NULL;
LPSPropValue lpProp;
LPABCONT lpABC = NULL;
LPMAPIPROP lpNewPABEntry = NULL;
LPSPropProblemArray lpPropProblems = NULL;
// Initialize MAPI Subsystem
hr = MAPIInitialize(NULL);
if (S_OK != hr) return;
// Logon to MAPI
hr = MAPILogonEx(0,NULL,NULL,MAPI_LOGON_UI,&lpSession);
if (FAILED(hr)) goto UnInit;
hr = lpSession->OpenAddressBook(0,NULL,0,&lpAddrbk);
if (FAILED(hr)) goto Cleanup0;
//Open PAB
SBinary pabEID;
hr = lpAddrbk -> GetPAB(&pabEID.cb, (LPENTRYID *)&pabEID.lpb);
if (FAILED(hr)) goto Cleanup0;
hr = lpAddrbk->OpenEntry(pabEID.cb,
(ENTRYID*)pabEID.lpb,
NULL,
MAPI_MODIFY,
&ulObjType,
(LPUNKNOWN FAR *)&lpABC);
if (FAILED(hr)) goto Cleanup0;
//Create a distributed list in PAB
hr = HrGetOneProp(lpABC, PR_DEF_CREATE_DL, &lpProp );
if (FAILED(hr)) goto Cleanup0;
hr = lpABC->CreateEntry(lpProp->Value.bin.cb,
(ENTRYID*)lpProp->Value.bin.lpb,
CREATE_CHECK_DUP_LOOSE,
&lpNewPABEntry);
if (FAILED(hr)) goto Cleanup0;
LPSPropValue pPropsMesg;
if (FAILED(hr = MAPIAllocateBuffer(sizeof(SPropValue),
(LPVOID *)&pPropsMesg)))
goto Cleanup0;
pPropsMesg[0].dwAlignPad = 0;
pPropsMesg[0].ulPropTag = PR_DISPLAY_NAME;
pPropsMesg[0].Value.LPSZ = "MyListCreatedByCode";
// Set the props on the object.
hr = lpNewPABEntry->SetProps(1, pPropsMesg, &lpPropProblems);
if (FAILED(hr)) goto Cleanup;
//Add entry to the list
AddEntryToDisList(lpAddrbk,
lpNewPABEntry,
"Wiggle",
"ALOT@JELLO.COM",
"SMTP",
"425-641-3309");
hr = lpNewPABEntry->SaveChanges(NULL);
if (FAILED(hr)) goto Cleanup;
Cleanup:
MAPIFreeBuffer((LPVOID)pPropsMesg);
Cleanup0:
if (lpNewPABEntry) lpNewPABEntry->Release();
if (lpABC) lpABC->Release();
if (lpAddrbk) lpAddrbk->Release();
lpSession->Logoff(0, 0, 0);
if (lpSession) lpSession->Release();
UnInit:
MAPIUninitialize();
}
void AddEntryToDisList(LPADRBOOK lpAddrbk,
LPMAPIPROP DisList,
LPTSTR lpszDisplayName,
LPTSTR lpszAddress,
LPTSTR lpszAddrType,
LPTSTR lpPhone)
{
HRESULT hr = S_OK;
ULONG ulCount = NULL;
ULONG ulObjType = NULL;
LPSRowSet pRows = NULL;
LPMAPIPROP lpNewEntry = NULL;
ULONG lpcbNewEntry;
LPENTRYID lppEIDNewEntry;
const unsigned long cProps = 4;
LPSPropValue pPropsMesg;
SizedSPropTagArray(2, TypeColumns) = {2, {PR_ADDRTYPE, PR_ENTRYID}};
LPDISTLIST lpDistList = NULL;
hr = DisList-> QueryInterface(IID_IDistList,
(void **)&lpDistList );
if (FAILED(hr = MAPIAllocateBuffer(sizeof(SPropValue) * cProps,
(LPVOID *)&pPropsMesg)))
goto Cleanup;
pPropsMesg[0].dwAlignPad = 0;
pPropsMesg[0].ulPropTag = PR_EMAIL_ADDRESS;
pPropsMesg[0].Value.LPSZ = lpszAddress;
pPropsMesg[1].dwAlignPad = 0;
pPropsMesg[1].ulPropTag = PR_DISPLAY_NAME;
pPropsMesg[1].Value.LPSZ = lpszDisplayName;
pPropsMesg[2].dwAlignPad = 0;
pPropsMesg[2].ulPropTag = PR_ADDRTYPE;
pPropsMesg[2].Value.LPSZ = lpszAddrType;
pPropsMesg[3].dwAlignPad = 0;
pPropsMesg[3].ulPropTag = PR_OFFICE_TELEPHONE_NUMBER;
pPropsMesg[3].Value.LPSZ = lpPhone;
if (FAILED(hr = lpAddrbk->CreateOneOff(pPropsMesg[1].Value.LPSZ,
pPropsMesg[2].Value.LPSZ,
pPropsMesg[0].Value.LPSZ,
0L,
&lpcbNewEntry,
&lppEIDNewEntry)))
goto Cleanup;
// Create a new entry. It is blank when created.
hr = lpDistList->CreateEntry(lpcbNewEntry,
(ENTRYID*)lppEIDNewEntry,
CREATE_CHECK_DUP_LOOSE,
&lpNewEntry);
if (FAILED(hr)) goto Cleanup;
if (S_OK == hr && lpNewEntry)
{
// Set some properties on the new Entry.
LPSPropProblemArray lpPropProblems = NULL;
hr = lpNewEntry->SetProps(cProps, pPropsMesg, &lpPropProblems);
if (FAILED(hr)) goto Cleanup;
// Explictly save the changes to the new entry
hr = lpNewEntry->SaveChanges(NULL);
if (MAPI_E_COLLISION == hr)
{
// We tried to add an entry that already exists
OutputDebugString("Collision occurred. The entry already exists.\n");
hr = S_OK; // Handled the error....
}
MAPIFreeBuffer((LPVOID)pPropsMesg);
}
Cleanup:
FreeProws(pRows);
if (lpNewEntry) lpNewEntry->Release();
if (lpDistList) lpDistList->Release();
}
REFERENCES
For additional information about creating an entry in the Personal Address Book, please see the following
article in the Microsoft Knowledge Base:
Q178172 How to Create an Entry in the Personal Address Book
Additional query words:
kbMAPI000 kbMAPI kbGrpMsg kbMsg kbVC
Keywords : kbMAPI kbMsg kbVC kbMAPI000 kbMAPI100 kbfaq kbGrpMsg
Version : WINDOWS:1.0
Platform : WINDOWS
Issue type : kbhowto