📄 mailclient2server.cpp
字号:
for (int k = 0; (k < folderNumber && arrayIndex < folderNumber); k++) {
if (deletedMessagesArray[arrayIndex]->size() == 0)
arrayIndex++;
else
break;
}
}
if (mailDeleted < allTot) {
MailItem* mailItem = (MailItem*)deletedMessagesArray[arrayIndex]->get(elementIndex);
if (mailItem) {
wchar_t *id = toWideChar(mailItem->getId());
if (!id){
LOG.error("Can't convert entryId");
return NULL;
}
mailClientData = new MailClientData(id);
delete [] id;
mailClientData->setParent(folderToSync[arrayIndex]);
}
if (elementIndex == deletedMessagesArray[arrayIndex]->size() -1) {
arrayIndex++;
elementIndex = -1;
}
}
LOG.debug("Exiting from getNextDeletedItem function");
return mailClientData;
}
BOOL openTmpFile(wchar_t* filename, FILE** f) {
BOOL ret = TRUE;
*f = _wfopen(filename, TEXT("w+b"));
if(f == NULL)
ret = FALSE;
return ret;
}
void closeTmpFile(FILE* f) {
if (f) {
fflush(f);
fclose(f);
}
}
int writeToTmpFile(FILE* f, char *ptr, int numBytes) {
int numWritten = fwrite(ptr, sizeof(char), numBytes, f);
return numWritten;
}
HRESULT MailClient2Server::GetAttachments(IMessage* pmsg, MailClientData* mcd) {
LOG.debug("Enter into GetAttachments function");
LPMAPITABLE pAttachTable = NULL;
// for each row in the attach table
LPSRowSet pRows = NULL;
int currentRow = 0;
char szBuf[2048];
ULONG ulNumChars;
HRESULT hr = pmsg->GetAttachmentTable(0, &pAttachTable);
EXIT_ON_FAILED(hr);
// iterate all the attachment
while(TRUE) {
hr = pAttachTable->QueryRows(1, 0, &pRows);
if (FAILED(hr) || pRows->cRows != 1)
break;
currentRow++;
// only one row is returned anyway
int nProps = pRows->aRow[0].cValues;
long num = -1;
long size = -1;
wchar_t* filename = NULL, *filenameTmp = NULL;
long msgstatus = -1;
IAttach* attach;
for (int i=0; i<nProps; i++) {
ULONG ulPT = pRows->aRow[0].lpProps[i].ulPropTag;
// loop through all the properties
// TCHAR* szPropName = ReverseLookup(ulPT);
switch (ulPT) {
case PR_ATTACH_NUM:
num = pRows->aRow[0].lpProps[i].Value.ul;
break;
case PR_ATTACH_SIZE:
size = pRows->aRow[0].lpProps[i].Value.ul;
break;
case PR_ATTACH_FILENAME:
filenameTmp = pRows->aRow[0].lpProps[i].Value.lpszW;
if (filenameTmp)
filename = wstrdup(filenameTmp);
break;
case PR_MSG_STATUS:
msgstatus = pRows->aRow[0].lpProps[i].Value.ul;
break;
case 0x30000003:
// pRows->aRow[0].lpProps[ii].Value.ul: sequence number
break;
default:
break;
} // switch
}
FreeProws(pRows);
pmsg->OpenAttach(num, NULL, 0, &attach);
// read IStream->Read way to write into an array of char.
IStream* pStream;
hr = attach->OpenProperty(PR_ATTACH_DATA_BIN, 0, NULL, ATTACH_BY_VALUE, (LPUNKNOWN *)&pStream);
EXIT_ON_FAILED(hr);
wchar_t tmpPath[64];
wchar_t tmpFileName[MAX_PATH]; // System constants for the path
GetTempPath(64, tmpPath);
GetTempFileName(tmpPath, TEXT("fun"), 0, tmpFileName);
FILE* f = NULL;
f = _wfopen(tmpFileName, TEXT("w+b"));
if (f == NULL)
goto FuncExit;
int m = 0;
do {
pStream->Read(szBuf, 2048, &ulNumChars);
writeToTmpFile(f, szBuf, ulNumChars);
m = m + ulNumChars;
} while (ulNumChars >= 2048);
closeTmpFile(f);
pStream->Release();
BodyPart bodyPart;
bodyPart.setFilename(_wcc(filename));
bodyPart.setContent(_wcc(tmpFileName));
bodyPart.setEncoding("base64"); // currently the ppc default
if (wcsstr(filename, TEXT(".wav"))) {
bodyPart.setMimeType("audio/x-wav");
} else if (wcsstr(filename, TEXT(".bmp"))) {
bodyPart.setMimeType("image/x-ms-bmp");
} else if (wcsstr(filename, TEXT(".gif"))) {
bodyPart.setMimeType("image/gif");
} else if (wcsstr(filename, TEXT(".jpg"))) {
bodyPart.setMimeType("image/jpg");
} else if (wcsstr(filename, TEXT(".psw"))) {
bodyPart.setMimeType("application/msword");
} else if (wcsstr(filename, TEXT(".htm"))) {
bodyPart.setMimeType("text/html");
}
mcd->getEmailData()->getEmailItem().addAttachment(bodyPart);
if (filename)
delete [] filename;
}
FuncExit:
// Free each element in the pRows buffer
FreeProws(pRows);
RELEASE_OBJ(pAttachTable);
LOG.debug("Exiting from GetAttachments function");
return hr;
}
HRESULT MailClient2Server::GetBody(IMessage* pmsg, wchar_t** body) {
LOG.debug("Enter into GetBody function");
IStream* pStream;
ULONG ulNumChars;
ULONG ulPos = 0;
HRESULT hr;
ULARGE_INTEGER plibNewPosition, startPosition;
LARGE_INTEGER dlibMove;
dlibMove.QuadPart= 0;
// Open the Stream to the body
hr = pmsg->OpenProperty(PR_BODY, NULL, STGM_READ, NULL,(LPUNKNOWN *) &pStream);
EXIT_ON_FAILED(hr);
pStream->Seek( dlibMove, STREAM_SEEK_END, &plibNewPosition);
*body = new wchar_t[(size_t)plibNewPosition.QuadPart + 1];
memset(*body, 0, ((size_t)plibNewPosition.QuadPart + 1)* sizeof(wchar_t));
pStream->Seek( dlibMove, STREAM_SEEK_SET, &startPosition);
hr = pStream->Read(*body, (size_t)plibNewPosition.QuadPart, &ulNumChars);
EXIT_ON_FAILED(hr);
pStream->Release();
return S_OK;
FuncExit:
LOG.error("Error reading body mail");
return -1;
}
wstring BuildAddress(IN LPTSTR pszDisplayName, IN LPTSTR pszEmailAddress)
{
LOG.debug("Enter into BuildAddress function");
wstring addressBuilt;
if (pszDisplayName == NULL && pszEmailAddress !=NULL ) {
addressBuilt.append(pszEmailAddress);
} else if (pszDisplayName!=NULL && pszEmailAddress==NULL) {
addressBuilt.append(TEXT("\""));
addressBuilt.append(pszDisplayName);
addressBuilt.append(TEXT("\""));
} else if (pszDisplayName!=NULL && pszEmailAddress!=NULL) {
addressBuilt.append(TEXT("\""));
addressBuilt.append(pszDisplayName);
addressBuilt.append(TEXT("\""));
addressBuilt.append(TEXT(" <"));
addressBuilt.append(pszEmailAddress);
addressBuilt.append(TEXT(">"));
} else {
addressBuilt.append(TEXT("UNKNOWN"));
}
LOG.debug("Exiting from BuildAddress function");
return addressBuilt;
}
// concatenate the recipients part
HRESULT MailClient2Server::GetRecipients(LPMESSAGE pMessage, char** to, char** cc, char** bcc) {
LOG.debug("Enter into GetRecipients function");
// for the recipient table
LPMAPITABLE pRecipientTable = NULL;
// for each row in the recipient table
LPSRowSet pRows = NULL;
int currentRow = 0;
int nTempLen = 0;
wstring::iterator it;
wstring szTempTO, szTempCC, szTempBCC;
// pRecipientTable->SetColumn does not work. Returning result is sth. like "NOT IMPLEMENTED".
// loop through all rows without limiting the columns
HRESULT hr = pMessage->GetRecipientTable(0, &pRecipientTable);
EXIT_ON_FAILED(hr);
// iterate all the recipients
while(TRUE) {
hr = pRecipientTable->QueryRows(1, 0, &pRows);
if (FAILED(hr) || pRows->cRows != 1) break;
currentRow++;
// only one row is returned anyway
int nProps = pRows->aRow[0].cValues;
TCHAR *szReceipName, *szReceipEmail;
ULONG ulReceipType;
szReceipEmail = NULL;
szReceipName = NULL;
ulReceipType = 0;
// loop through all the properties
for (int ii=0; ii<nProps; ii++) {
ULONG ulPT = pRows->aRow[0].lpProps[ii].ulPropTag;
// TCHAR* szPropName = ReverseLookup(ulPT);
switch (ulPT) {
case PR_EMAIL_ADDRESS:
szReceipEmail = pRows->aRow[0].lpProps[ii].Value.LPSZ;
break;
case PR_RECIPIENT_TYPE:
ulReceipType = pRows->aRow[0].lpProps[ii].Value.ul;
break;
case PR_DISPLAY_NAME:
szReceipName = pRows->aRow[0].lpProps[ii].Value.LPSZ;
break;
case PR_ADDRTYPE:
// pRows->aRow[0].lpProps[ii].Value.LPSZ. Always "SMTP"
break;
case 0x30000003:
// pRows->aRow[0].lpProps[ii].Value.ul: sequence number
break;
default:
break;
} // switch
}
// now let us build the String
wstring szAddr = BuildAddress(szReceipName, szReceipEmail);
if (ulReceipType==MAPI_TO) {
szTempTO.append(szAddr);
szTempTO.append(TEXT(","));
} else if (ulReceipType==MAPI_CC) {
szTempCC.append(szAddr);
szTempCC.append(TEXT(","));
} else if (ulReceipType==MAPI_BCC) {
szTempBCC.append(szAddr);
szTempBCC.append(TEXT(","));
}
// Free each element in the pRows buffer
FreeProws(pRows);
} // end of the loop through all recipients
// remove the trailing ",", if any
int nLen = 0;
nLen = szTempTO.size();
if (nLen > 0) {
it = szTempTO.end() -1;
if (*it == L',')
*it = L'\0';
}
nLen = szTempCC.size();
if (nLen > 0) {
it = szTempCC.end() -1;
if (*it == L',')
*it = L'\0';
}
nLen = szTempBCC.size();
if (nLen > 0) {
it = szTempBCC.end() -1;
if (*it == L',')
*it = L'\0';
}
nTempLen = szTempTO.size();
if (nTempLen>0) {
*to = toMultibyte(szTempTO.c_str());
}
nTempLen = szTempCC.size();
if (nTempLen>0) {
*cc = toMultibyte(szTempCC.c_str());
}
nTempLen = szTempBCC.size();
if (nTempLen>0) {
*bcc = toMultibyte(szTempBCC.c_str());
}
FuncExit:
// Free each element in the pRows buffer
FreeProws(pRows);
RELEASE_OBJ(pRecipientTable);
LOG.debug("Exiting from GetRecipients function");
return hr;
}
HRESULT MailClient2Server::GetAllMessages(ENTRYLIST& entryList, LPMAPIFOLDER m_pInBoxFolder,
const wchar_t* path, wchar_t folderId) {
LOG.debug("Enter into GetAllMessages function");
HRESULT hr;
UINT nEntries = 0;
SBinary sbEntry;
LPMAPITABLE pContentsTable = NULL;
LPSRowSet pRows = NULL;
// We define a SPropTagArray array here using the SizedSPropTagArray Macro
// This enum will allows us to access portions of the array by a name instead of a number.
// If more tags are added to the array, appropriate constants need to be added to the enum.
hr = m_pInBoxFolder->GetContentsTable( 0, &pContentsTable);
EXIT_ON_FAILED(hr);
hr = pContentsTable->SetColumns((LPSPropTagArray)&Columns, 0);
EXIT_ON_FAILED(hr);
while(!FAILED(hr = pContentsTable->QueryRows(1, 0, &pRows)))
{
if(pRows->cRows == 0)
break;
sbEntry = pRows->aRow[0].lpProps[eePR_ENTRYID].Value.bin;
if(entryList.cValues)
{
PVOID pOldBuffer = entryList.lpbin;
MAPIAllocateBuffer(sizeof(SBinary) * (entryList.cValues + 1), (LPVOID*)&(entryList.lpbin));
memmove((LPVOID*)entryList.lpbin, pOldBuffer, sizeof(SBinary) * entryList.cValues);
MAPIFreeBuffer(pOldBuffer);
}
else {
MAPIAllocateBuffer(sizeof(SBinary), (LPVOID*)&(entryList.lpbin));
}
entryList.lpbin[entryList.cValues].cb = sbEntry.cb;
MAPIAllocateBuffer(sbEntry.cb, (LPVOID*)&(entryList.lpbin[entryList.cValues].lpb) );
memmove(entryList.lpbin[entryList.cValues].lpb, sbEntry.lpb, sbEntry.cb);
entryList.cValues++;
// Free each element in the pRows buffer
FreeProws(pRows);
hr = S_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -