Copying a Message from One Folder to Another
| Nik Okuntseff |
MS Exchange Server Programming |
Copying a Message from One Folder to Another
Sometimes it is necessary to copy a message programmatically from one folder
to another. This task may be accomplished by opening source and destination
folders, locating a message in the source folder, and then using the IMAPIFolder::CopyMessages
function. Take a look at the following sample (Folders/Copy):
#include <afxwin.h>
#include <edk.h>
int main()
{
/*
* Algorithm:
* 1. Logon to MAPI and open message
store.
* 2. Locate and open source and target
folders.
* 3. Scan source folder to find the
message to copy.
* 4. Call the IMAPIFolder::CopyMessages.
*/
// Change if necessary
char * pszSourceFolder = "@PR_IPM_SUBTREE_ENTRYID\\Deleted
Items";
char * pszTargetFolder = "@PR_IPM_SUBTREE_ENTRYID\\Inbox";
char * pszMsgSubject = "RE: Computer";
HRESULT hr;
LPMAPISESSION pSession = NULL;
// Initialize MAPI
hr = MAPIInitialize(NULL);
if (FAILED(hr)) throw -1;
// Obtain MAPI session
hr = MAPILogonEx(0, // Handle
to parent window
"MS Exchange Settings", // Profile name
NULL, // Password
MAPI_NEW_SESSION |
MAPI_EXTENDED |
MAPI_LOGON_UI, // Logon flags
&pSession); // Resulting MAPI
session
if (FAILED(hr)) throw -1;
// Find default message store
ULONG cbDefStoreEid = 0;
LPENTRYID pDefStoreEid = NULL;
hr = HrMAPIFindDefaultMsgStore(pSession, &cbDefStoreEid,
&pDefStoreEid);
if (FAILED(hr)) throw -1;
// Open default message store
LPMDB pDefMsgStore = NULL;
hr = pSession->OpenMsgStore(0, cbDefStoreEid, pDefStoreEid,
NULL,
MDB_WRITE | MAPI_DEFERRED_ERRORS,
&pDefMsgStore);
if (FAILED(hr)) throw -1;
// Open source folder
LPMAPIFOLDER pSourceFolder = NULL;
hr = HrMAPIOpenFolderEx(pDefMsgStore,
'\\',
pszSourceFolder,
&pSourceFolder);
if (FAILED(hr)) throw -1;
// Open target folder
LPMAPIFOLDER pTargetFolder = NULL;
hr = HrMAPIOpenFolderEx(pDefMsgStore,
'\\',
pszTargetFolder,
&pTargetFolder);
if (FAILED(hr)) throw -1;
// Get contents table for the source folder.
LPMAPITABLE pContentsTable = NULL;
hr = pSourceFolder->GetContentsTable(0, &pContentsTable);
if (FAILED(hr)) throw -1;
// Get row count
ULONG ulRows = 0;
hr = pContentsTable->GetRowCount(0, &ulRows);
if (FAILED(hr)) throw -1;
assert(ulRows > 0);
// Get all rows
SRowSet * pRowSet = NULL;
hr = pContentsTable->QueryRows(ulRows, 0, &pRowSet);
assert(ulRows == pRowSet->cRows);
// Scan all rows to find a message with our subject
SRow row;
ULONG cbEid = 0;
LPENTRYID pEid = NULL;
LPUNKNOWN pCurrentMsg = NULL;
for (ULONG ul = 0; ul < ulRows; ul++)
{
row = pRowSet->aRow[ul];
for (ULONG ulIndex = 0; ulIndex
< row.cValues; ulIndex++)
{
if (PR_SUBJECT == row.lpProps[ulIndex].ulPropTag)
{
char * pszCurSubject = row.lpProps[ulIndex].Value.lpszA;
int iRes = memcmp(pszMsgSubject, pszCurSubject, strlen(pszMsgSubject));
if (0 == iRes)
{
// We have found our message!
// Get PR_ENTRYID
SBinary bin;
ZeroMemory(&bin, sizeof(bin));
for (ULONG ulNewIndex = 0; ulNewIndex < row.cValues; ulNewIndex++)
{
if (PR_ENTRYID == row.lpProps[ulNewIndex].ulPropTag)
{
bin = row.lpProps[ulNewIndex].Value.bin;
break;
}
}
// Construct ENTRYLIST
ENTRYLIST list;
list.cValues = 1;
list.lpbin = &bin;
// Call IMAPIFolder::CopyMessages method
hr = pSourceFolder->CopyMessages(&list,
NULL,
pTargetFolder,
NULL,
NULL,
0);
if (FAILED(hr)) throw -1;
goto cleanup;
}
break;
}
}
}
cleanup:
// Cleanup
if (pRowSet)
MAPIFreeBuffer(pRowSet);
if (pContentsTable)
pContentsTable->Release();
if (pTargetFolder)
pTargetFolder->Release();
if (pSourceFolder)
pSourceFolder->Release();
if (pDefMsgStore)
pDefMsgStore->Release();
if (pDefStoreEid)
MAPIFreeBuffer(pDefStoreEid);
if (pSession)
pSession->Release();
::MAPIUninitialize();
return 0;
}
The code is self-illustrative.
[ Contents |
Home
]
Send comments and suggestions to niko@wrconsulting.com
Copyright © 1997-1998 by Nik Okuntseff
|