Thursday, September 13, 2007

MAPI : how to delete a message(or move it into trash)?

Well.
I think deleting a message is rather easy,but i find moving a message instead to trash or wastebasket folder is not that easy.
Here is a function i use to determine the wastebasket folder.So i can use IFolder,CopyMessages routine with MAPI_MOVE flag to move my message there!

HRESULT GetWastebasketForFolder(IMAPISession *m_pSession,LPMAPIFOLDER pFolder,
LPMAPIFOLDER* ppfldrWastebasket)
{
HRESULT hr = E_FAIL;
IMsgStore* pms = NULL;
ULONG cItems;
ULONG rgtagsFldr[] = { 1, PR_OWN_STORE_ENTRYID };
ULONG rgtagsMsgStore[] = { 1, PR_IPM_WASTEBASKET_ENTRYID };
LPSPropValue rgprops = NULL;

// This method assumes that the CALLER already logged on to a MAPISession
if (!m_pSession)
CHR(E_FAIL);

// Now request the PR_OWN_STORE_ENTRYID on the folder. This is the
// ENTRYID of the message store that owns the folder object.
hr = pFolder->GetProps((LPSPropTagArray)rgtagsFldr, MAPI_UNICODE, &cItems, &rgprops);
CHR(hr);

CBR(PR_OWN_STORE_ENTRYID == rgprops[0].ulPropTag);

// Now open the message store object.
hr = m_pSession->OpenEntry(rgprops[0].Value.bin.cb,
(LPENTRYID)rgprops[0].Value.bin.lpb,
NULL, 0, NULL, (LPUNKNOWN*)&pms);
CHR(hr);

MAPIFreeBuffer(rgprops);
rgprops = NULL;

// Get the ENTRYID of the wastebasket for the message store
hr = pms->GetProps((LPSPropTagArray)rgtagsMsgStore, MAPI_UNICODE, &cItems, &rgprops);
CHR(hr);

// Now open the correct wastebasket and return it to the caller.
CBR(PR_IPM_WASTEBASKET_ENTRYID == rgprops[0].ulPropTag);

hr = m_pSession->OpenEntry(rgprops[0].Value.bin.cb,
(LPENTRYID)rgprops[0].Value.bin.lpb,
NULL, 0, NULL, (LPUNKNOWN*)ppfldrWastebasket);
CHR(hr);

Error:
MAPIFreeBuffer(rgprops);
RELEASE_OBJ(pms);

return hr;
}



and you call it like this :
GetWastebasketForFolder(pSession,pFolder,&pWasteBasket);


pFolder is a folder where you message is.
And moving is done like this.
pFolder->CopyMessages(&lst, NULL, pWasteBasket, 0, NULL, MAPI_MOVE);


Probably you might know that lst is ENTRYLIST and you construct it like this!
lst.cValues = 1;
sbin.cb = pspvEntryID->Value.bin.cb;
sbin.lpb = (LPBYTE) pspvEntryID->Value.bin.lpb;
lst.lpbin = &sbin;


And pspvEntryID is what i get from calling GetProps to getting this message property ids.

2 comments:

Adrian said...
This comment has been removed by the author.
Adrian said...

Thanks for the post - you saved me a lot of time.

techwireless.blogspot.com