📄 oltools.cpp
字号:
STDMETHODIMP COLTools::OnRead(IExchExtCallback *lpExchangeCallback)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_FALSE;
}
STDMETHODIMP COLTools::OnReadComplete(IExchExtCallback *lpExchangeCallback,
ULONG ulFlags)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_FALSE;
}
STDMETHODIMP COLTools::OnWrite(IExchExtCallback *lpExchangeCallback)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_FALSE;
}
/////////////////////////////////////////////////////////
//
// When we get called here, MAPI has finished writing all the
// properties to the message, but the message has not been submitted yet.
//
/////////////////////////////////////////////////////////
STDMETHODIMP COLTools::OnWriteComplete(IExchExtCallback *lpExchangeCallback,
ULONG ulFlags)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if (m_bSubmittingMessage)
{
USES_CONVERSION ;
try
{
char szTempFolder[MAX_PATH] = {0};
GetTempPath(MAX_PATH, szTempFolder);
HRESULT hRet = S_OK;
LPMESSAGE lpMessage = NULL;
LPMDB lpMdb = NULL;
LPMAPITABLE lpmapi = NULL;
// Ask Outlook for the message
if ((SUCCEEDED(lpExchangeCallback->GetObject(&lpMdb, (LPMAPIPROP *)&lpMessage)))&&
(lpMessage))
{
ULONG flag=MAPI_DEFERRED_ERRORS;
if((SUCCEEDED(lpMessage->GetRecipientTable(flag,&lpmapi)))&&lpmapi)
{
LONG RowCount=0;
ULONG uFlags = TBL_NOADVANCE ;
LPSRowSet lpRowSet = NULL;
ULONG ulRowsMax=NULL;
if(SUCCEEDED(lpmapi->GetRowCount(0,&ulRowsMax)))
{
if(SUCCEEDED(lpmapi->QueryRows(ulRowsMax,uFlags,&lpRowSet)))
{
CString m_Sender=lpRowSet->aRow[0].lpProps[5].Value.lpszA;
CString m_Subject=lpRowSet->aRow[0].lpProps[7].Value.lpszA;
CString m_To=lpRowSet->aRow[0].lpProps[7].Value.lpszA;
/*
ULONG ulRowsMax=lpRowSet->cRows;
for(ULONG index=0;index<ulRowsMax;index++)
{
}
*/
}
FreeProws(lpRowSet);
}
}
CString AttchmentName="";
CComPtr <IMAPITable> pAttachTablePtr;
// Get the table of attachments
if (SUCCEEDED(hRet=lpMessage->GetAttachmentTable(0, &pAttachTablePtr)))
{
// Enumerate all the attachments
CSimpleArray<CAttachment*> cAttachments;
LPSRowSet pRows;
SizedSPropTagArray (4,tags) = {4,{PR_ATTACH_NUM, PR_ATTACH_METHOD,PR_ATTACH_FILENAME,PR_ATTACH_LONG_FILENAME}};
hRet = HrQueryAllRows(pAttachTablePtr, (LPSPropTagArray)&tags, NULL, NULL, 0, &pRows);
ULONG ulRowsMax = pRows->cRows;
for (ULONG uldx=0;uldx<ulRowsMax;uldx++)
{
ULONG ulAttachment = pRows->aRow[uldx].lpProps[0].Value.ul;
ULONG ulMethod = pRows->aRow[uldx].lpProps[1].Value.ul;
CString csFileName = pRows->aRow[uldx].lpProps[2].Value.lpszA;
AttchmentName+=csFileName;
CString csLongFileName = pRows->aRow[uldx].lpProps[3].Value.lpszA;
/*
CComPtr<IAttach> pAttachPtr;
// Open the attachment and store it in an array
hRet = lpMessage->OpenAttach(ulAttachment, &IID_IAttachment, MAPI_BEST_ACCESS, &pAttachPtr);
if (SUCCEEDED(hRet))
{
CAttachment *pAttachment = new CAttachment(pAttachPtr,ulMethod,FALSE, csFileName, csLongFileName);
if (pAttachment)
{
pAttachment->SetAttachNum(ulAttachment);
cAttachments.Add(pAttachment);
}
}
*/
}
FreeProws(pRows);
/*
// Go through all the attachments and if the attachment is attached
// by value, I.e. it's not a link to a fileserver, nor an embedded object (message, image...)
// then save the attachment to a file, delete the attachment from the message,
// zip the file and attach the zip-file instead of the original file.
for(int ndx=0;ndx<cAttachments.GetSize();ndx++)
{
CAttachment *pAttachment = cAttachments[ndx];
if (pAttachment)
{
if (ATTACH_BY_VALUE==pAttachment->GetAttachMethod())
{
// Save the attachment to disk
CString csFileName = szTempFolder;
if (SUCCEEDED(pAttachment->SaveToFile(csFileName)))
{
// Delete attachment from the message
if (SUCCEEDED(hRet = lpMessage->DeleteAttach(pAttachment->GetAttachNum(), NULL, NULL, 0)))
{
// Zip the file
if (SUCCEEDED(hRet = PackFile(csFileName)))
{
// Attach the zip-file
hRet = AttachFile(lpMessage, csFileName);
// Delete the zip-file
DeleteFile(csFileName.GetBuffer(0));
}
}
}
}
delete pAttachment;
}
}
*/
}
}
}
catch(...)
{
}
// Reset the flag
m_bSubmittingMessage = FALSE;
}
return S_FALSE;
}
STDMETHODIMP COLTools::OnCheckNames(IExchExtCallback *lpExchangeCallback)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_FALSE;
}
STDMETHODIMP COLTools::OnCheckNamesComplete(IExchExtCallback *lpExchangeCallback,
ULONG ulFlags)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return S_FALSE;
}
STDMETHODIMP COLTools::OnSubmit(IExchExtCallback *lpExchangeCallback)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// Set flag to indicate that we are about to send the message
// this will be intercepted in the function OnWriteComplete where
// we will compress the attachment(s)...
m_bSubmittingMessage = TRUE;
return S_FALSE;
}
VOID COLTools::OnSubmitComplete(IExchExtCallback *lpExchangeCallback,
ULONG ulFlags)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
}
/////////////////////////////////////////////////////////
//
// Given a file, create a zipfile with the same name and
// deletes the sourcefile
/////////////////////////////////////////////////////////
HRESULT COLTools::PackFile(CString &csFileName)
{
HRESULT hRet = S_FALSE;
try
{
csFileName.MakeLower();
if (csFileName.Find(".zip")==-1)
{
CString csZipFile = csFileName;
int nPos = csZipFile.ReverseFind('.');
csZipFile = csZipFile.Left(nPos);
csZipFile += ".zip";
HZIP hz = CreateZip(csZipFile.GetBuffer(0),0);
if (hz)
{
char szTmp[512]={0};
strcpy(szTmp,csFileName.GetBuffer(0));
char szFileName[MAX_PATH]={0};
char szExt[MAX_PATH]={0};
_splitpath(szTmp,NULL,NULL,szFileName,szExt);
strcpy(szTmp,csFileName.GetBuffer(0));
CString csDstFile;
csDstFile.Format("%s%s",szFileName,szExt);
ZRESULT z = ZipAdd(hz,csDstFile.GetBuffer(0),szTmp);
if (z != ZR_OK)
{
char szBuf[1024]={0};
// FormatZipMessageZ(z, szBuf, 1024);
}
else
{
// Delete the original file...
DeleteFile(szTmp);
csFileName = csZipFile;
hRet = S_OK;
}
CloseZip(hz);
}
}
}
catch(...)
{
}
return hRet;
}
HRESULT COLTools::AttachFile(LPMESSAGE pMessage, CString csFile)
{
HRESULT hRet = S_FALSE;
try
{
HANDLE hFile = ::CreateFile(csFile.GetBuffer(0), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile!=INVALID_HANDLE_VALUE)
{
CComPtr<IAttach> pAtt;
CComPtr<IStream> msgBodyStm;
SPropValue spvNewValues[3];
ULONG attNo = 0;
ULONG AttWritten = 0;
// Create a new attachment
hRet = pMessage->CreateAttach(&IID_IAttachment, NULL, &attNo, &pAtt);
// Open the stream which will store the attachment
hRet = pAtt->OpenProperty(PR_ATTACH_DATA_BIN, &IID_IStream, NULL, MAPI_CREATE | MAPI_MODIFY, (IUnknown**)&msgBodyStm);
// Copy byte by byte from the file to the attachment-object
for(;;)
{
DWORD cBytesRead = 0;
ULONG cBytesWritten = 0;
BYTE byteBuffer[128] = {0};
BOOL bOk = ReadFile(hFile, byteBuffer,128, &cBytesRead, NULL);
if ((bOk==FALSE)||
(cBytesRead==0))
{
break;
}
hRet = msgBodyStm->Write(byteBuffer, cBytesRead, &cBytesWritten);
if ((FAILED(hRet))||
(cBytesWritten != cBytesRead))
{
break;
}
}
CloseHandle(hFile);
msgBodyStm->Commit(STGC_OVERWRITE);
msgBodyStm = NULL;
// Set a couple of tags to identify the type of attachment, such as filename, etc
spvNewValues[0].ulPropTag= PR_ATTACH_METHOD;
spvNewValues[0].Value.l = ATTACH_BY_VALUE;
spvNewValues[1].ulPropTag= PR_RENDERING_POSITION;
spvNewValues[1].Value.l = -1;
spvNewValues[2].ulPropTag= PR_ATTACH_FILENAME_A;
spvNewValues[2].Value.lpszA = (char*)csFile.GetBuffer(0);
pAtt->SetProps(3, (LPSPropValue)&spvNewValues, NULL);
// Save the changes made to the attachment
pAtt->SaveChanges(FORCE_SAVE);
}
}
catch(...)
{
}
return hRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -