在上一篇里面讲述了如何为一条MESSAGE设置附件,下面将继续关于附件的话题,利用上一个例子,我们接下来来看看如何获取一条MESSAGE的附件信息。下面将通过两个帮助函数来完成:
BOOL MAPIHelp_SaveAttachFile( LPATTACH pAttach, LPCTSTR szFile )
作用:读取单个附件文件内容,并保存到指定位置
pAttach: 附件对象
szFile: 保存文件名
BOOL MAPIHelp_GetAttachment( IMessage* pMsg, LPCTSTR szFilePath )
作用:获取一条Message的全部附件,并保存到指定目录下
pMsg: 目标消息对象
szFilePath: 目标目录
下面来看看具体实现:
BOOL MAPIHelp_SaveAttachFile( LPATTACH pAttach, LPCTSTR szFile )
{
if( NULL == pAttach || NULL == szFile )
return FALSE;
HANDLE hFile = INVALID_HANDLE_VALUE;
IStream* pstmAttachment = NULL;
char * pBuffer = NULL;
int i = 0;
DWORD dwWrite = 0;
BOOL
bRet = FALSE;
ULONG ulRead = 0;
//打开附件,获取IStream对象,用于获取文件内容,根据MSDN的解释,这里只支持PR_ATTACH_DATA_BIN属性。
if(FAILED(pAttach->OpenProperty (PR_ATTACH_DATA_BIN, NULL, STGM_READ, MAPI_MODIFY,
reinterpret_cast <IUnknown **> (&pstmAttachment))))
{
goto EXIT;
}
//创建目标文件
hFile = ::CreateFile(szFile, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(INVALID_HANDLE_VALUE == hFile)
{
goto EXIT;
}
//缓冲区,用于文件拷贝
pBuffer = new char[4096];
if(NULL == pBuffer)
{
goto EXIT;
}
//附件内容拷贝
while(SUCCEEDED(pstmAttachment->Read(pBuffer, 4096, &ulRead)))
{
if(ulRead <= 0)
break;
::WriteFile(hFile, pBuffer, ulRead, &dwWrite, NULL);
}
bRet = TRUE;
EXIT:
if(INVALID_HANDLE_VALUE != hFile)
{
::CloseHandle(hFile);
}
if(NULL != pBuffer)
{
delete []pBuffer;
}
if(NULL != pstmAttachment)
{
pstmAttachment->Release();
}
return
bRet;
}
BOOL MAPIHelp_GetAttachment( IMessage* pMsg, LPCTSTR szFilePath )
{
if( NULL == pMsg || NULL == szFilePath )
return FALSE;
LPMAPITABLE pAttachTbl = NULL;
SRowSet* psrs = NULL;
LPATTACH pAttach = NULL;
LONG lAttachNum = 0;
BOOL
bRet = FALSE;
//获取附件列表
if(FAILED(pMsg->GetAttachmentTable(0, &pAttachTbl)))
{
goto EXIT;
}
//接下来的查询过程是不是很眼熟?
while(SUCCEEDED(pAttachTbl->QueryRows (1, 0, &psrs)))
{
//即使查询返回成功,可能记录数也为0,需要排除这种CASE
if (NULL == psrs || psrs->cRows != 1)
{
break;
}
TCHAR szFile[MAX_PATH];
//遍历所有属性,找出附件ID和名称
for(int i = 0; i < (int)(psrs->aRow[0].cValues); ++i)
{
if(PR_ATTACH_NUM == psrs->aRow[0].lpProps[i].ulPropTag)
{
//找到附件ID,并打开附件对象
if(FAILED(pMsg->OpenAttach(psrs->aRow[0].lpProps[i].Value.l,
NULL,
MAPI_BEST_ACCESS,
&pAttach)))
{
goto EXIT;
}
lAttachNum = psrs->aRow[0].lpProps[i].Value.l;
}
else if(PR_ATTACH_FILENAME == psrs->aRow[0].lpProps[i].ulPropTag)
{
//获取附件名称,生成保存路径
_stprintf( szFile, _T("%s%s"), szFilePath, psrs->aRow[0].lpProps[i].Value.lpszW );
}
}
if(pAttach)
{
//保存文件
MAPIHelp_SaveAttachFile( pAttach, szFile );
pAttach->Release();
pAttach = NULL;
}
FreeProws(psrs);
psrs = NULL;
}
bRet = TRUE;
EXIT:
if(NULL != psrs)
{
FreeProws(psrs);
}
if(NULL != pAttach)
{
pAttach->Release();
}
if(NULL != pAttachTbl)
{
pAttachTbl->Release();
}
return
bRet;
}
外面调用时候很简单,只需要获取IMessage对象,再调用MAPIHelp_GetAttachment即可。
好了关于附件的内容讲完了,惯例,欢迎大家拍砖。
无聊客(blog.csdn.net\yzx0023)
2006-7-26